Funktionen mit Defaultwerten



  • Hey C++ Kommunity,

    bin noch recht frisch was das Programmieren angeht und habe einige AUfgaben schon angegangen. Beim einer AUfgabe muste ich eine Trapez berechnung machen. Hat auch super geklappt 🙂

    #define _USE_MATH_DEFINES
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    double Trapez_flaeche(double a, double b=0., double gamma=90., double c=0.)
    {
    //	cout << a << " " << b << " " << gamma << " " << c << " ";
    	if(b==0) {c=b=a;}
    	if(c==0) {c=a;};
    //	cout << a << " " << b << " " << gamma << " " << c << " ";
    	double h = b*sin (gamma*2.0*M_PI/360.0);
    	return (1.0/2.0) * (a+c) * h;
    }
    
    int main (void)
    {
    	cout << Trapez_flaeche(2.0, 4.0, 45.0, 3.5) << endl;
    	cout << Trapez_flaeche(3.0, 4.0, 45.0) << endl;
    	cout << Trapez_flaeche(4.0, 5.0) << endl;
    	cout << Trapez_flaeche(2.0) << endl;
    }
    

    Nun habe ich etwas herumgespielt und getüftlt aber es klappt einfach nicht wenn ich die Main Funktion als erstes anstelle.

    1>AG_1-6.cpp(23): error C2660: 'Trapez_flaeche': Funktion akzeptiert keine 3 Argumente
    1>AG_1-6.cpp(24): error C2660: 'Trapez_flaeche': Funktion akzeptiert keine 2 Argumente
    1>AG_1-6.cpp(25): error C2660: 'Trapez_flaeche': Funktion akzeptiert keine 1 Argumente

    #define _USE_MATH_DEFINES
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    double Trapez_flaeche(double a, double b, double gamma, double c);
    
    int main (void)
    {
    	cout << Trapez_flaeche(2.0, 4.0, 45.0, 3.5) << endl;
    	cout << Trapez_flaeche(3.0, 4.0, 45.0) << endl;
    	cout << Trapez_flaeche(4.0, 5.0) << endl;
    	cout << Trapez_flaeche(2.0) << endl;
    }
    
    double Trapez_flaeche(double a, double b=0., double gamma=90., double c=0.)
    {
    //	cout << a << " " << b << " " << gamma << " " << c << " ";
    	if(b==0) {c=b=a;}
    	if(c==0) {c=a;};
    //	cout << a << " " << b << " " << gamma << " " << c << " ";
    	double h = b*sin (gamma*2.0*M_PI/360.0);
    	return (1.0/2.0) * (a+c) * h;
    }
    

    Kann mir jm. helfen was ich falsch mache weil normalerweiße macht man ja die Funktionsdefinition zu beginn und dann kann man den Funktionsrumpf doch verschieben?



  • Die Defaultargumente (nicht Defaultwerte) gehören in die Deklaration, nicht in die Definition.



  • Oh mann und schon klappt es 😞 Ich glaube ich sollte eine längere Pause einlegen 🙂 Danke

    #define _USE_MATH_DEFINES
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    double Trapez_flaeche(double a, double b=0., double gamma=90., double c=0.);
    
    int main (void)
    {
    	cout << Trapez_flaeche(2.0, 4.0, 45.0, 3.5) << endl;
    	cout << Trapez_flaeche(3.0, 4.0, 45.0) << endl;
    	cout << Trapez_flaeche(4.0, 5.0) << endl;
    	cout << Trapez_flaeche(2.0) << endl;
    }
    
    double Trapez_flaeche(double a, double b, double gamma, double c)
    {
    //	cout << a << " " << b << " " << gamma << " " << c << " ";
    	if(b==0) {c=b=a;}
    	if(c==0) {c=a;};
    //	cout << a << " " << b << " " << gamma << " " << c << " ";
    	double h = b*sin (gamma*2.0*M_PI/360.0);
    	return (1.0/2.0) * (a+c) * h;
    }
    

Anmelden zum Antworten