[gelöst] Linkerfehler bei Destruktor
-
EDIT:
Ich war zu blind zu sehen das ich diese Funktionen nur deklariert aber nicht definiert hatte...Bei folgendem Code
#ifndef SHAPES_H_ #define SHAPES_H_ #include <string> #include <cassert> class AbstractShape { public: AbstractShape(void); virtual ~AbstractShape(void); virtual double area () const = 0; void setDescription(std::string const & str) { m_description = str; } private: std::string m_description; }; class RectShape : public AbstractShape { public: RectShape(void) : AbstractShape(), m_width(0), m_height(0) { setDescription("Rectangular Shape"); } RectShape(const RectShape & s) { this->m_width = s.m_width; this->m_height = s.m_height; } const RectShape& operator=(const RectShape& rhs) { if (this != &rhs) { this->m_width = rhs.m_width; this->m_height = rhs.m_height; } return *this; // return self-reference so cascaded assignment works } virtual ~RectShape(void); void setSize(double width, double height) { setWidth(width); setHeight(height); } void setWidth(double width) { assert(width > 0); m_width = width; } void setHeight(double height) { assert(height > 0); m_height = height; } double area () const { return (m_width * m_height); } private: double m_width; double m_height; }; #endifund
// main.cpp #include "Shapes.h" int main(int argc, char** argv) { RectShape rectShape; }bekomme ich die Linkerfehler:
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: virtual __thiscall AbstractShape::~AbstractShape(void)" (??1AbstractShape@@UAE@XZ)" in Funktion "__unwindfunclet$??0RectShape@@QAE@XZ$0".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall AbstractShape::AbstractShape(void)" (??0AbstractShape@@QAE@XZ)" in Funktion ""public: __thiscall RectShape::RectShape(void)" (??0RectShape@@QAE@XZ)".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: virtual __thiscall RectShape::~RectShape(void)" (??1RectShape@@UAE@XZ)" in Funktion ""public: virtual void * __thiscall RectShape::`scalar deleting destructor'(unsigned int)" (??_GRectShape@@UAEPAXI@Z)".warum?
-
Weil du den Destruktor deklarierst, aber nicht implementierst. Klar beschwert sich da der Linker...
Denn der Destruktor wird ja definitiv gebraucht, um die Instanz wieder geregelt ins Nirvana zu befördern.
-
Das Gleiche gilt für den Konstruktor und den anderen Destruktor. Aber dazu müsste man die Fehlermeldung lesen...