template <class T> class Tree
{
protected:
//--------------------------------------------------------
// inner class Node
// a single Node from a binary tree
//--------------------------------------------------------
template <class T> class Node
{
public:
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;
}
**我想做一个会返回Node指针的函数, 我认为因为它导致我错误的模板, 我有其他方法来实现该功能吗?**
答案 0 :(得分:2)
这里有一个错字:
Node<T>* searchTree<T>::minimum(Node<T>* current);
searchTree
应为SearchTree
且资本为S
。
答案 1 :(得分:1)
哇,修复你的例子是一个挑战。
#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;
}
我认为你进入了更复杂的事情然后你想到了,你标记的线不是唯一的问题。
查找依赖名称和模板。
答案 2 :(得分:0)
有一些问题......你的Node不需要是模板,因为它已经存在;你有一个错误searchTree<T>::minimum
(大写)...节点是树的一部分,所以它需要范围。
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;
}