-
template errors
Code:
template<class Type>
class Tree
{
private:
struct Node
{
Type item;
// node[0] = parent;
// node[1] = left child;
// node[2] = right child;
Node * node[3];
};
Node * root;
public:
Tree():root(NULL) {}
Node* search_tree(Type x);
};
template<class Type>
Tree<Type>::Node * Tree<Type>::search_tree(Type x)
{
if( this == NULL) return NULL;
if(this->item == x)
return this;
else if( this->item < x )
return this->node[1].search_tree(x);
else
return this->node[2].search_tree(x);
}
That class template keeps giving me the errors;
Code:
error: expected constructor, destructor, or type conversion before ‘*’ token
does anyone know how to fix that error?
-
Code:
template<class Type>
typename Tree<Type>::Node * Tree<Type>::search_tree(Type x)
In rough terms, without the typename keyword present, the standard says that the compiler must interpret TreeType<Node> as a non-type name while parsing the template code. That is the reason for your error message.
As to the reason the standard says that - view finding and understanding that as a homework challenge. The reason is not trivial, and a complete explanation gives most people a headache.
-
Thank you grumpy for the answer. I keep forgetting the use of "typename" for a user-defined type inside a class.