c++ - 无效的协变返回类型错误

标签 c++ inheritance tree

<分区>

我在重写派生类中的基类函数时遇到以下错误。

./inc/rbtree.h:16:18: error: invalid covariant return type for ‘virtual RBNode* RBTree::get_root()’
./inc/tree.h:25:24: error:   overriding ‘virtual Node* Tree::get_root()’

rbtree.h

class RBNode;

class RBTree: public Tree
{
    protected:
    public:
        RBTree();
        RBNode *root;
        RBNode * get_root();
        void insert_into_tree();
        //void delete_from_tree();
};

tree.h如下所示

class Node;

class Tree
{
    protected:
        Node * root;
        list<int> treedata;
    public:
        Tree();
        /* This is where it gives error */
        virtual Node * get_root();
        void set_root(Node *root_node);
        void insert_into_tree();
        void delete_from_tree();
        void print_tree();
};

更多信息:RBNode 派生自 Node。 现在,我读到这个 ​​article它说完全可以覆盖基类函数以返回派生类型实例。

我也在 SO 上进行了搜索,但有关此的问题并没有多大帮助。如果在 C++ 中允许,为什么会失败。

最佳答案

看来您是在转发声明 RBNode。当编译器看到 RBTree 时,它​​还不知道 RBNode 是一个节点。

关于c++ - 无效的协变返回类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29817748/

相关文章:

c++ - 删除控件 wxWidgets

c++ - 声明 const 成员函数的语法返回一个裸函数指针,没有 typedefs?

python 如何使用中序/前/后/无递归遍历二叉搜索树?

data-structures - 程序范围的数据结构?

c++ - 在 pcl 注册方法中获取不被对应距离拒绝的内点。

java - 调用方法时 JNI C++ UnsatisfiedLinkError

java - 在 Java 中将子类设置为不可序列化

java - Android 的 onBackPressed() 方法如何在没有在 MainActivity 中显式调用的情况下被调用?

c++ - 模板类继承链接器错误

c# - SQL数据层次结构