c++ - Virtual functions and double inheritance -


i've been trying solve problem hours, couldn't find solution. code example:

class icolor { // color interface public:     virtual void print(); };  class color : public icolor { // abstract color class  };  class rgb : public color { // color implementation public:     void print()     {         std::cout << "hi";     } };  int main() {     icolor* col = new rgb();     col->print();     return 0; } 

however, result of compilation linker errors:

/home/snndaj/ccnvqhgl.o:(.rodata._zti5color[_zti5color]+0x8): undefined reference `typeinfo icolor' /home/snndaj/ccnvqhgl.o:(.rodata._ztv5color[_ztv5color]+0x8): undefined reference `icolor::print()' collect2: error: ld returned 1 exit status 

(not)working online example: https://ideone.com/yikywe

change base class have pure virtual member:

class icolor { public:     virtual void print() = 0; }; 

as code stands, declaring icolor::print never defining it, leads unresolved reference linker complaining about. pure-virtual function requires no definition, , indeed no definition makes sense in case, since every leaf class must override method.

in fact, need virtual destructor:

virtual ~icolor() {} 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -