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();