c++ - Using virtual destructors gives error -


i'm making little program school. want add virtual destructor class 'iobject', 'square' , 'rotatedsquare'. when do, gives error. not possible in case or how here? here's code:

interface iobject :

#ifndef iobject_h_ #define iobject_h_ #include "point.h"  class iobject { public:     virtual const point& getposition()const = 0;     virtual double getsize()const = 0;     //virtual ~iobject();  };  #endif 

class square

#ifndef square_h_ #define square_h_ #include "iobject.h" #include "point.h" #include <iostream> class square : public iobject{ private:     point position;     double size; public:     square(const point& position, const double size):position(position),size(size){};     virtual const point& getposition() const {return this->position;}     virtual double getsize() const {return this->size;}     friend std::ostream& operator<<(std::ostream& out, const square& s);     //virtual ~square();  protected:     virtual void print(std::ostream& out) const; };  #endif  

class rotatedsquare

#ifndef rotatedsquare_h_ #define rotatedsquare_h_ #include "square.h"  class rotatedsquare : public square{ private:     double angle; public:     rotatedsquare(const point& position, const double size, const double angle): square(position,size),angle(angle){};     double getangle() const {return this->angle;}     //virtual ~rotatedsquare(); protected:     virtual void print(std::ostream& out) const; };  #endif 

the error :

undefined symbols architecture x86_64:   "square::~square()", referenced from:       _main in practice.o   "vtable rotatedsquare", referenced from:       rotatedsquare::rotatedsquare(point const&, double, double) in practice.o   note: missing vtable means first non-inline virtual member function has no definition.   "vtable square", referenced from:       square::square(point const&, double) in practice.o   note: missing vtable means first non-inline virtual member function has no definition.   "vtable iobject", referenced from:       iobject::iobject() in practice.o   note: missing vtable means first non-inline virtual member function has no definition. 

the main class :

#include <iostream> #include "point.h" #include "iobject.h" #include "square.h" #include "rotatedsquare.h"  int main() {       iobject* s1 = new square(point(1,4),2.2);      std::cout << s1->getposition() << std::endl;      std::cout << s1->getsize() << std::endl;   square s2 = square(point(4,5),5);   square* s3 = new rotatedsquare(point(5,5),8,60);   std::cout << s2 << std::endl;   std::cout << *s3; return 0; } 

you need define each of virtual destructors declare. assuming don't want destructor more default behavior, can make definition declaration: instead of:

virtual ~iobject(); 

use

virtual ~iobject() {} 

if want significant work in destructor, or prefer definitions in cpp rather hpp, define destructor in cpp file (keep declaration without definition in hpp)

iobject::~iobject() { // whatever needs done } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -