Reputation: 157
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.
Upvotes: 1
Views: 80
Reputation: 53027
Is Node
a subclass of BSTree
? If so, it's not in scope in the return type and so you have to qualify it:
template <typename Item, typename Key>
typename BSTree<Item,Key>::Node* BSTree<Item,Key>::copy(Node* root)
If you have C++11 then auto
works too:
template <typename Item, typename Key>
auto BSTree<Item,Key>::copy(Node* root) -> Node
Upvotes: 2