c++ - 为什么我的嵌套类被视为抽象类?

标签 c++ abstract-class nested-class

我有一个包含私有(private)嵌套实现的抽象基类。当我尝试实例化非抽象嵌套实现时,Visual C++ 出现以下错误:

错误 C2259:“node::empty_node”:无法实例化抽象类(第 32 行)

据我所知,我已经覆盖了基类的所有抽象成员

代码如下:

using namespace boost;
template<typename K, typename V>
class node {
protected:
    class empty_node : public node<K,V> {
    public:
        bool is_empty(){ return true; }
        const shared_ptr<K> key() const { throw empty_node_exception; }
        const shared_ptr<V> value() const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> left() const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> right() const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) const {
            return shared_ptr<node<K,V>>();
        }
        const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) const { throw empty_node_exception; }
        const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) const { return shared_ptr<node<K,V>>(this); }
    };
    static shared_ptr<node<K,V>> m_empty;
public:
    virtual bool is_empty() = 0;
    virtual const shared_ptr<K> key() = 0;
    virtual const shared_ptr<V> value() = 0;
    virtual const shared_ptr<node<K,V>> left() = 0;
    virtual const shared_ptr<node<K,V>> right() = 0;
    virtual const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value) = 0;
    virtual const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) = 0;
    virtual const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) = 0;


    static shared_ptr<node<K,V>> empty(){
        if(NULL == m_empty.get()){
            m_empty.reset(new empty_node());
        }
        return m_empty;
    }
};

最佳答案

函数签名不匹配。基类“节点”中的纯虚成员函数不是常量;派生类“empty_node”中的函数是常量。

您要么需要将基类虚函数设为 const,要么从派生类的成员函数中删除 const 限定符。

关于c++ - 为什么我的嵌套类被视为抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1547870/

相关文章:

c++ - 一些反汇编 c++ 代码我很困惑

java - 嵌套类(静态内部)生命周期/范围

java - 访问静态成员类

c++ - 为什么在使用重载的 insertopm (<<) 运算符时会收到 SIGSEGV?

c++ - VarBstrFromI4 改变了程序中其他一些 BSTR 的值

c++ - 创建 C++ 程序时符号加载(源信息剥离)错误

Java - 抽象问题

java - 有没有办法制作一个非抽象但必须被覆盖的方法?

c++ - 引用而不是指向抽象类的指针

java - 如何访问同名的外部类变量?