Ueberladungskonstruktor && Veerbung



  • Hi Leute,

    stehe widermal vor einem Problem...
    Kann mir jemand helfen wie ich diese Fehlermeldung beheben kann!!

    Wie sollte die Deklaration in der Header Datei gegliedert seien, damit ich im main programm ohne probleme der instanz werte via parameter übergeben kann... (Zeile 49 und Zeile 66)

    Fehlermedlung des Compilers:

    "d:\visualstudio2012\vorlesungen\vererb_ueberlad_kopier_stand_konstruktor\vererb_ueberlad_kopier_stand_konstruktor\vererb_ueberlad_kopier_stand_konstruktor.cpp(18): error C2661: 'CEnkel::CEnkel': Keine überladene Funktion akzeptiert 2 Argumente
    1>d:\visualstudio2012\vorlesungen\vererb_ueberlad_kopier_stand_konstruktor\vererb_ueberlad_kopier_stand_konstruktor\vererb_ueberlad_kopier_stand_konstruktor.cpp(19): error C2661: 'CSohn::CSohn': Keine überladene Funktion akzeptiert 3 Argumente
    ========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 0 aktuell, 0 übersprungen ==========
    "
    
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    class COpa {
    
    private: 
    
    		//Attribute
    	int x;
    
    public:
    
    		//STANDARDKONSTRUKTOR
    		//
    	COpa();
    		//KOPIERKONSTRUKTOR
    		//
    	COpa(const COpa& ); 
    
    		//UEBERLADUNGSKONSTRUKTOR
    		//Wird nur aufgerufen, wenn im Hauptprogramm der Instanz ein Wert uberegeben wird!!!!!
    	COpa(int); 
    
    		//Methode zur Ausgabe des Attributes der Klasse COpa
    		//
    	void foo(); 
    
    };
    
    // Klasse COpa wird an Klasse CSohn vererbt!!!
    
    class CSohn:public COpa {     //Klasse CSohn wurde von der Klasse COpa geerbt!!!!
    
    private:
    	int y;
    
    public:
    
    		//STANDARDKONSTRUKTOR	
    		//
    	CSohn();
    		//KOPIERKONSTRUKTOR
    		//
    	CSohn(const CSohn& );
    		//UEBERLADUNGSKONSTRUKTOR
    		//
    	CSohn(int, int):COpa(int); 
    		//AUSGABE
    		//
    	void foo();
    
    };
    
    class CEnkel : public CSohn {
    private:
    	int z;
    public:
    
    		//Konstruktor
    	CEnkel();
    		//Kopierkonstruktor
    	CEnkel(const CEnkel&);
    		//Überladenerkonstruktor
    	CEnkel(int,int,int):CSohn(int,int); 
    		//Ausgabe
    	void foo();
    
    };
    
    //Quelldatei zum Projekt Vererb_Ueberlad_Kopier_Stand_Konstruktor
    //29.10.2013
    
    #include "stdafx.h"
    #include <iostream>
    #include "header.h"
    
    using namespace std;
    
    	//STANDARDKONSTRUKTOR
    	//
    	COpa::COpa()  {
    
    		x = 1;
    	}
    
    	//KOPIERKONSTRUKTOR
    	//
    	COpa::COpa(const COpa& original) {
    
    		x = original.x;			// Hier in diesem Fall, wird sozusagen, die "Kopie" vom Original im Attribut x erstellt/ueberwiesen!!!
    	}
    
    	//UEBERLADUNGSKONSTRUKTOR
    	//Wird nur aufgerufen, wenn im Hauptprogramm der Instanz ein Wert uberegeben wird!!!!!
    	COpa::COpa(int neu) {
    
    		x = neu;
    	}
    
    	//Ausgabe von Copa
    	void COpa:: foo() {
    
    		cout << "x: " << x << endl;
    	}
    
    	//VERERBUNG
    	//KLASSE CSohn(...)
    
    	//STANDARDKONSTRUKTOR	
    	//
    	CSohn::CSohn() {
    		y = 1;
    	}
    
    	//KOPIERKONSTRUKTOR
    	//
    	CSohn::CSohn(const CSohn& original) {
    
    		y = original.y;
    	}
    
    	//UEBERLADUNGSKONSTRUKTOR
    	//
    
    	CSohn::CSohn(int x, int y):COpa(x) {
    		this->y = y;
    	}
    
    	//AUSGABE
    	//
    	void CSohn:: foo() {
    		cout << "y: " << y;
    	}
    
    	//Konstruktor
    	CEnkel::CEnkel() {
    		z=1;
    	}
    
    	//Kopierkonstruktor
    	CEnkel::CEnkel(const CEnkel& original) {
    
    		z = original.z;
    	}
    
    	//Überladenerkonstruktor
    	CEnkel::CEnkel(int x,int y,int z):CSohn(x,y){
    
    		this->z = z;
    
    	}
    	//Ausgabe
    	void CEnkel:: foo(){
    		cout << "z: " << z << endl;
    	}
    
    #include "stdafx.h"
    #include <iostream>
    #include "header.h"
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	COpa   irgendwas(5);
    	CSohn  irgendwas_neues(3,4); 
    	CEnkel  irgendwas_anderes_neues(2,4,2); 
    
    	irgendwas_anderes_neues.foo();
    
    	return 0;
    }
    


  • Hab jetzt grad keinen Compiler zur Hand, aber das sieht für mich etwas fremd aus (z.B. "CSohn(int, int):COpa(int);" in der Header-Datei. Lass doch mal das ":COpa(int)" weg, hast Du doch schon!
    Gruß Helmut



  • Vielen Dank für die Antwort. Du hast recht, ich habe dass ":Copa(int)", vom Code CSohn(int, int):COpa(int);, weggelassen; es funktiort. Supe Danke 🙂



  • Hmm, ich glaube hier gibt es ein kleines Verständnisproblem, was Vererbung angeht.

    Kommt dynamische Vererbung (ist das mit virtual) zum Einsatz, muss immer ein virtueller Destruktor vorhanden sein. Du bekommmst sonst Probleme beim Abbau von Objekten.

    Ich gebe mal ein allgemeines Beispiel, dass die Probleme aufklären sollte. Ich versuche es auch gleichzeitig anhand eines praktischen Beispiels zu zeigen.

    Hier wäre die Definition, also Vector.hxx (ich gehöre zu den Leuten, die .cxx und .hxx verwenden)

    #include <string>
    
    // --- Vector2 ---
    class Vector2 {
    public:
      // --- Vector2: public data structures ---
      float x;
      float y;
    
      // --- constructors and deconstructors ---
      Vector2();
      Vector2(const float& xx, const float& yy);
      Vector2(const Vector2& rhs);
      virtual ~Vector2();
    
      // --- Vector2: base operators ---
      Vector2& operator=(const Vector2& rhs);
      bool operator==(const Vector2& rhs) const;
      bool operator!=(const Vector2& rhs) const;
    
      // --- Vector2: default methods ---
      virtual std::string string() const;
    };
    
    // --- Vector3 ---
    class Vector3 : public Vector2 {
    public:
      // --- Vector3: publioc data structures ---
      float z;
    
      // --- Vector3: constructors and deconstructors ---
      Vector3();
      Vector3(const Vector2& vec, const float&& zz = 0.0);
      Vector3(const float& xx, const float&& yy, const float&& zz);
      Vector3(const Vector3& rhs);
      virtual ~Vector3();
    
      // --- Vector3: base operators ---
      Vector3& operator=(const Vector3& rhs);
      bool operator==(const Vector3& rhs) const;
      bool operator!=(const Vector3& rhs) const;
    
      // --- Vector3: public methods ---
      virtual std::string string() const;
    };
    

    Und hier kommt die Implementierung, also Vector.cxx ...

    #include <sstream>
    #include "Vector.hxx"
    
    // --- Vector2: constructors and deconstructors ---
    Vector2::Vector2()
    : x(0.0), y(0.0)
    {
    }
    
    Vector2::Vector2(const float& xx, const float& yy)
    : x(xx), y(yy)
    {
    }
    
    Vector2::Vector2(const Vector2& rhs)
    : x(rhs.x), y(rhs.y)
    {
    }
    
    Vector2::~Vector2()
    {
    }
    
    // --- Vector2: base operators ---
    Vector2& Vector2::operator=(const Vector2& rhs)
    {
      if (this != &rhs)
      {
        x = rhs.x;
        y = rhs.y;
      }
    
      return *this;
    }
    
    bool Vector2::operator==(const Vector2& rhs) const
    {
      return x == rhs.x &&
             y == rhs.y;
    }
    
    bool Vector2::operator!=(const Vector2& rhs) const
    {
      return !(*this == rhs);
    }
    
    // --- Vector2: public methods ---
    std::string Vector2::string() const
    {
      std::stringstream result;
    
      result << x << ", "
             << y;
    
      return result.str();
    }
    
    // --- Vector3: constructors and deconstructors ---
    Vector3::Vector3()
    : Vector2(0.0, 0.0), z(0.0)
    {
    }
    
    Vector3::Vector3(const Vector2& vec, const float& zz)
    : Vector2(vec), z(zz)
    {
    }
    
    Vector3::Vector3(const float& xx, const float& yy, const float& zz)
    : Vector2(xx, yy), z(zz)
    {
    }
    
    Vector3::Vector3(const Vector3& rhs)
    : Vector2(rhs.x, rhs.y), z(rhs.z)
    {
    }
    
    Vector3::~Vector3()
    {
    }
    
    // --- Vector3: base operators ---
    Vector3& Vector3::operator=(const Vector3& rhs)
    {
      if (this != &rhs)
      {
        Vector2::operator=(rhs);
        z = rhs.z;
      }
    
      return *this;
    }
    
    bool Vector3::operator==(const Vector3& rhs) const
    {
      return Vector2::operator==(rhs) &&
             z == rhs.z;
    }
    
    bool Vector3::operator!=(const Vector3& rhs) const
    {
      return !(*this == rhs);
    }
    
    // --- Vector3: public methods ---
    std::string Vector3::string() const
    {
      std::stringstream result;
    
      result << Vector2::string() << ", "
             << z;
    
      return result.str();
    }
    

    Das sollte eine halbwegs elegante Implementierung sein, die in Sachen Vererbung alles beachtet, was es zu beachten gibt. Und ich hoffe mal, dass sich kein Typo eingeschlichen hat. Das sollte man problemlos 1:1 verwenden können.

    Wenns Fragen gibt, dann nur zu 😃


Anmelden zum Antworten