In the following piece of code, I have a doubt and an error which I am unable to resolve:
When defining the function
root()
outsideclass BST
, why do I have to usetypename bst::Node*
? HadBST
not been templated, I would have been able to use justBST::Node
right?While overloading
operator<<
forBST
outside the class throws the following error:
Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream >&, BST::Node*)", referenced from: BST::inorderTreeWalk(BST::Node*) in bst_raw.cpp.o ld: symbol(s) not found for architecture x86_64
Can someone please help clarify my doubts?
#define T template <typename K, typename V>
#define bst BST<K,V>
T
class BST {
public:
class Node;
private:
Node* _root;
public:
BST():_root(NULL) {};
bool empty() ;
Node* root();
void set_root(Node* z);
void insert(K key, V value);
void inorderTreeWalk(Node* x);
};
T
class bst::Node {
public:
Node(K k, V v) : _key(k), _val(v) {};
K key() const { return _key; }
V val() const { return _val; }
Node* left() { return l;}
void set_left(Node* n) { l = n;}
Node* right() { return r;}
void set_right(Node* n) { r = n; }
Node* parent();
void set_parent(Node* n) { p = n; }
friend ostream &operator<< (ostream &os, Node* x);
private:
Node *l, *r, *p;
K _key;
V _val;
};
T
typename bst::Node* bst::root() { return _root; } // why typename?
T
ostream &operator<<(ostream &os, typename bst::Node& x) {
os << x.key();
return os;
} // runs into an error