为什么我的嵌套类被视为抽象类?
我有一个抽象基类,其中包含一个私有嵌套实现。当我尝试实例化非抽象嵌套实现时,Visual C++ 给出以下错误:
error 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;
}
};
I have an abstract base class which contains a private nested implementation. visual c++ is giving me the following error when I try to instantiate the non-abstract nested implementation:
error C2259: 'node::empty_node' : cannot instantiate abstract class (line 32)
as far as I can tell, I've overridden all the abstract members of the base class
Code follows:
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;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数签名不匹配。基类“node”中的纯虚成员函数不是 const;派生类“empty_node”中的函数是 const。
您需要将基类虚函数设置为 const,或者从派生类的成员函数中删除 const 限定符。
The function signatures don't match. The pure virtual member functions in the base class 'node' are not const; the functions in the derived class 'empty_node' are const.
You either need to make the base class virtual functions const or remove the const qualifier from the member functions in the derived class.
您的嵌套类缺少
key
、value
、left
、right
、的非常量版本添加
、删除
和搜索
方法。您的
const
函数不会被覆盖。Your nested class is missing a non-const versions of
key
,value
,left
,right
,add
,remove
andsearch
methods.Your
const
functions are not overrides.