ctor-problem



  • Erst mal will ich mich entschuldigen dass ich keinen adequaten titel gewählt habe, aber mir ist halt nicht klar wie mein problem geartet ist und wie ich das ausdrücken kann. Also hier erst mal der code:

    #include <iostream>
    class Two
    {
    public:
    	Two()
    	{
    		std::cout<<"dummy 1";
    	}
    };
    class Three
    {
    public:
    	Three()
    	{
    		std::cout<<"dummy 3";
    	}
    };
    class Four
    {
    public:
    	Four()
    	{
    		std::cout<<"dummy 4";
    	}
    };
    class One:public Two
    {
    public:
    	One()
    	{
    		Three three();
    		std::cout<<"dummy 2";
    	}
    private:
    	Four _four;
    };
    
    int main()
    {
    	One one();
    	return 0;
    }
    

    Und jetzt will ich mal mein problem schildern:
    das programm soll die dummy strings drucken tut es aber nicht (keine compiler- fehlermeldungen)

    Schon mal im voraus vielen dank
    +++



  • +++ schrieb:

    int main()
    {
    	One one();
    	return 0;
    }
    

    die '()' hinter 'one' sind zuviel 😉
    edit: weiss auch nicht warum es compiled. der compiler denkt wohl es wär ein funktions-prototyp



  • int main()
    {
        One one();
        return 0;
    }
    

    =>

    int main()
    {
        One one;
        return 0;
    }
    

    Dein "One one()" definiert lediglich eine Funktion names one die als Returnwert
    ein Objekt vom Typ One liefert. 🙂



  • statt

    int main() 
    { 
        One one(); 
        return 0; 
    }
    

    muß du das schreiben:

    int main() 
    { 
        One one;   // dein objekt ist dich keine funktion
        return 0; 
    }
    

    das gleiche gilt für:

    One() 
        { 
            Three three();  // <-- hier auch ohne '()'
            std::cout<<"dummy 2"; 
        }
    

    //edit: etwas zu langsam 😃



  • Vielen Dank,
    Hab an alles mögliche gedacht nur an dass nicht.
    (ich glaub ich bin zu doof)
    +++


Anmelden zum Antworten