Reputation: 2849
I'm constructing a generic binary tree which includes a struct, TreeNode. I implement a function rootPtr() which returns the pointer to the root of the tree. binarytree.h is simply the following:
template <class T>
class BinaryTree {
public:
typedef struct TreeNode {
T item;
TreeNode *leftChildPtr; // pointer to left child
TreeNode *rightChildPtr;
public:
TreeNode(const T& nodeItem,
TreeNode *left,
TreeNode * right)
: item(nodeItem), leftChildPtr(left), rightChildPtr(right) {
}
TreeNode(const T & nodeItem)
: item(nodeItem) {
leftChildPtr = NULL;
rightChildPtr = NULL;
}
} TN; //end treenode
//Function prototype
TN* rootPtr() const;
private:
TN *root;
} //end binarytree
This is the snippet from binarytree.cpp which results error.
template <class T>
BinaryTree<T>::TN* BinaryTree<T>::rootPtr() {
return root;
}
Compiler complains error: expected constructor, destructor, or type conversion before '*' token
I've seen this error before and handled it by using like BinaryTree::
but this time since it's generic, I don't know what should I do. Any help would be appreciated.
Upvotes: 0
Views: 209
Reputation: 30969
Try adding typename
before the return type in the function definition, since TN
is a member of a dependent class.
Upvotes: 1
Reputation: 3187
Simplified rule of thumb: you cannot define member functions when the class has a template in a separate cpp file. Try putting that together with the class definition, on the header ;)
Upvotes: 3