I am writing a function to copy a templated binary tree. So far, I have this:
template <typename Item, typename Key>
Node* BSTree<Item,Key>::copy(Node* root) {
if(root == NULL) return NULL;
Node* left;
Node* right;
Node* to_return;
left = copy(root->left());
right = copy(root->right());
to_return = new Node(root->data());
to_return->left() = left;
to_return->right() = right;
return to_return;
}
But when I try to compile the program, I get multiple errors that I can’t figure out how to solve. All of them occur on the line right after the template declaration.
1) error C2143:syntax error: missing’;’ before’*’
2) error C4430: missing type specifier – int assumed
3) error C2065: ‘Item’ : undeclared identifier
4) error C2065: ‘Key’ : undeclared identifier
All of my other functions in the program compile correctly and have no problem with the template so I’m not really sure why this one does. It is already declared in the header file and definitely has a return type assigned to it so I’m stumped.
Is
Nodea subclass ofBSTree? If so, it’s not in scope in the return type and so you have to qualify it:If you have C++11 then
autoworks too: