error C2143: syntax error : missing ';' before '<'
template <class T> class Tree
{
protected:
//--------------------------------------------------------
// inner class Node
// a single Node from a binary tree
//--------------------------------------------------------
template <class T> class Node
{
public:
开发者_JS百科 Node * left;
Node * right;
T value;
Node(T val)
: value(val), left(NULL), right(NULL){}
Node(T val, Node<T> * l, Node<T> * r)
: value(val), left(l), right(r){}
}; //end of Node class
Node<T> * root;
public:
Tree() {root=NULL;} // initialize tree
};
template <class T>
class SearchTree : public Tree<T>
{
public:
// protocol for search trees
void add(T value);
bool search(T value) {return search(root,value); }
void remove(T value);
Node<T>* minimum(){return minimum(root); }
Node<T>* findSuccessor(Node<T>* x);
Node<T>* findParent(T val);
private:
void add(Node<T> * current, T val);
bool search(Node<T>* current, T val);
Node<T>* minimum(Node<T>* current);
};
// THE ERROR IS HERE
template <class T>
Node<T>* searchTree<T>::minimum(Node<T>* current);
{
while (current->left != NULL)
curent= current->left;
return current;
}
** i want to do a function that will return a pointer of Node, i think because the template it causing me an error, do i have another way to implement the function?**
There's a typo here:
Node<T>* searchTree<T>::minimum(Node<T>* current);
searchTree
should be SearchTree
with a capital S
.
Wow it was a challenge to fix your example.
#include <iostream>
template <class T> class Tree
{
protected:
//--------------------------------------------------------
// inner class Node
// a single Node from a binary tree
//--------------------------------------------------------
template <class T1> class Node
{
public:
Node * left;
Node * right;
T1 value;
Node(T1 val) : value(val), left(NULL), right(NULL){}
Node(T1 val, Node<T1> * l, Node<T1> * r) : value(val), left(l), right(r){}
}; //end of Node class
Node<T> * root;
public:
Tree() {root=NULL;} // initialize tree
};
template < class T >
class SearchTree : public Tree< T >
{
public:
// protocol for search trees
void add(T value);
bool search(T value) {return search(Tree< T >::root,value); }
void remove(T value);
typename Tree< T >::template Node< T >* minimum(){return minimum(Tree< T >::root); }
typename Tree< T >::template Node< T >* findSuccessor(typename Tree< T >::template Node< T >* x);
typename Tree< T >::template Node< T >* findParent(T val);
private:
void add(typename Tree< T >::template Node< T >* current, T val);
bool search(typename Tree< T >::template Node< T >* current, T val);
typename Tree< T >::template Node< T >* minimum(typename Tree< T >::template Node< T >* current);
};
// THE ERROR IS HERE
template <class T>
typename Tree< T >::template Node< T >*
SearchTree< T >::minimum( typename Tree< T >::template Node< T > * current )
{
while (current->left != NULL)
current= current->left;
return current;
}
int main()
{
SearchTree< int > obj;
}
I think you got into more complex thing then you thought, and the line you marked is not the only problem.
Lookup dependant names and templates.
There were a few things wrong... your Node does not need to be a template as it is already; you had a typo with searchTree<T>::minimum
(capitalize)... Node is part of Tree, so it requires scope.
template <class T> class Tree
{
protected:
//--------------------------------------------------------
// inner class Node
// a single Node from a binary tree
//--------------------------------------------------------
class Node
{
public:
Node * left;
Node * right;
T value;
Node(T val)
: value(val), left(NULL), right(NULL){}
Node(T val, Node * l, Node * r)
: value(val), left(l), right(r){}
}; //end of Node class
Node * root;
public:
Tree() {root=NULL;} // initialize tree
};
template <class T>
class SearchTree : public Tree<T>
{
public:
// protocol for search trees
void add(T value);
bool search(T value) {return search(root,value); }
void remove(T value);
Node* minimum(){return minimum(root); }
Node* findSuccessor(Node* x);
Node* findParent(T val);
private:
void add(Node * current, T val);
bool search(Node* current, T val);
Node* minimum(Node* current);
};
template <class T>
SearchTree<T>::Tree::Node* SearchTree<T>::minimum(SearchTree<T>::Tree::Node* current)
{
while (current->left != NULL)
curent= current->left;
return current;
}
精彩评论