为什么我会遇到未解决的外部问题?
我正在用 C++ 编写一个不可变的二叉搜索树。我的终止节点由单个空节点表示。我的编译器(Visual C++)似乎在解析保存我的单例的受保护静态成员时遇到问题。我收到以下错误:
error LNK2001: unresolved external symbol "protected: static class boost::shared_ptr > node::m_empty" (?m_empty@?$node@HH@@1V?$shared_ptr@V?$node@HH @@@boost@@A)
我假设这意味着它无法解析类型节点的静态 m_empty 成员。这是正确的吗?如果是这样我该如何修复它?
代码如下:
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() { throw cant_access_key; }
const shared_ptr<V> value() { throw cant_access_value; }
const shared_ptr<node<K,V>> left() { throw cant_access_child; }
const shared_ptr<node<K,V>> right() { throw cant_access_child; }
const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value){
return shared_ptr<node<K,V>>();
}
const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) { throw cant_remove; }
const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) { 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(m_empty.get() == NULL){
m_empty.reset(new empty_node());
}
return m_empty;
}
};
我的树的根初始化为:
shared_ptr<node<int,int>> root = node<int,int>::empty();
I am writing an immutable binary search tree in c++. My terminating nodes are represented by a singleton empty node. My compiler (visual c++) seems to be having trouble resolving the protected static member that holds my singleton. I get the following error:
error LNK2001: unresolved external symbol "protected: static class boost::shared_ptr > node::m_empty" (?m_empty@?$node@HH@@1V?$shared_ptr@V?$node@HH@@@boost@@A)
I am assuming this means it cant resolve the static m_empty member for the type node. Is this correct? If so how do I fix it?
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() { throw cant_access_key; }
const shared_ptr<V> value() { throw cant_access_value; }
const shared_ptr<node<K,V>> left() { throw cant_access_child; }
const shared_ptr<node<K,V>> right() { throw cant_access_child; }
const shared_ptr<node<K,V>> add(const shared_ptr<K> &key, const shared_ptr<V> &value){
return shared_ptr<node<K,V>>();
}
const shared_ptr<node<K,V>> remove(const shared_ptr<K> &key) { throw cant_remove; }
const shared_ptr<node<K,V>> search(const shared_ptr<K> &key) { 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(m_empty.get() == NULL){
m_empty.reset(new empty_node());
}
return m_empty;
}
};
the root of my tree is initialized as:
shared_ptr<node<int,int>> root = node<int,int>::empty();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,您需要为静态成员提供一个定义点。但是,由于它是模板的成员,因此语法将比之前建议的更复杂一些。如果我没有遗漏任何内容,它应该如下所示
。如果需要,您还可以在此声明中提供一个初始化程序(或多个初始化程序)。
As others said, you need to provide a definition point for your static member. However, since it is a member of a template, the syntax is going to be a bit more complex then what was suggested before. If I'm not missing anything, it should look as follows
You can also provide an initializer (or initializers) in this declaration, if necessary.
m_empty
是静态的,因此您需要一个源 (.cpp) 文件,其中包含以下内容:注意:我原来的答案不正确,并且没有考虑到这是一个模板。这是AndreyT在他的回答中给出的答案;我已使用正确答案更新了此答案,因为这是已接受的答案并显示在页面顶部。请支持 AndreyT 的答案,而不是这个。
m_empty
is static and so you'll need to have a source (.cpp) file with something like the following:Note: My original answer was incorrect and did not take into account that this was a template. This is the answer that AndreyT gave in his answer; I've updated this answer with the correct answer because this is the accepted answer and appears at the top of the page. Please upvote AndreyT's answer, not this one.
您需要在 .cpp 文件中初始化 m_empty 变量。
You need to initialize the m_empty variable in your .cpp file.