c++ - Derived class not recognized as covariant -
so creating huffman tree, , having hard time overriding function, , believe due covariance issue. here hierarchy having hard time in code:
class treeinterface { public: treeinterface() {} virtual ~treeinterface() {} virtual nodeinterface * getrootnode() const = 0; }; class tree : treeinterface { public: tree() {} virtual ~tree() {} node* getrootnode() { return treeroot; } private: node* treeroot; };
those work fine, next block has issues.
class huffmaninterface{ public: huffmaninterface() {} virtual ~huffmaninterface() {} virtual bool createtree(string filename) = 0; virtual string encodemessage(string toencode) = 0; virtual treeinterface * gettree() = 0; virtual map<char, string> getencodings() = 0; }; class huffman : huffmaninterface{ public: huffman() {} ~huffman() {} bool huffman::createtree(string filename){ } string huffman::encodemessage(string toencode){ } string huffman::decodemessage(string todecode){ } tree* huffman::gettree(){ } map<char, string> huffman::getencodings(){ }
so problem apparently in gettree() function, giving following error
invalid covariant return type ‘virtual tree* huffman::gettree()’: tree * gettree();
but far know, tree* should valid covariant of treeinterface*. replacing tree* treeinterface* makes program compile, it's not need in actual program. appreciated!
class tree : treeinterface { ... };
is equivalent
class tree : private treeinterface { ... };
you need make inheritance public
.
class tree : public treeinterface { ... };
Comments
Post a Comment