主题:关于树的类
自己写的树类,编译老是有这样的错:
f:\tufu\tree\treenode.h(29) : error C2953: 'TwoTreeNode' : template class has already been defined
f:\tufu\tree\treenode.h(29) : see declaration of 'TwoTreeNode'
f:\tufu\tree\treenode.h(39) : error C2995: 'GetTreeNode' : template function has already been defined
f:\tufu\tree\treenode.h(28) : see declaration of 'GetTreeNode'
Error executing cl.exe.
Tree.exe - 2 error(s), 0 warning(s)
代码如下:
template <class T>
class TwoTreeNode
{
public:
T data;
TwoTreeNode<T> * leftchild, * rightchild;
TwoTreeNode(void) {leftchild=rightchild=NULL;}
TwoTreeNode(const T& item)
{
data = item;
leftchild = rightchild = NULL;
}
TwoTreeNode(const T& item,TwoTreeNode *left,TwoTreeNode *right)
{
data = item;
leftchild = left;
rightchild = right;
}
void FreeTreeNode(TwoTreeNode<T> * p){delete p;}
TwoTreeNode<T> * GetTreeNode(T item,TwoTreeNode<T> * left=NULL,TwoTreeNode<T> * right=NULL);
};
template <class T>
TwoTreeNode<T> * TwoTreeNode<T>::GetTreeNode(T item,TwoTreeNode<T> * left,TwoTreeNode<T> * right)
{
TwoTreeNode<T> * p;
p = new TwoTreeNode<T>(item,left,right);
if(p==NULL)
cerr << "Memory allocation failure! \n";
return p;
}
请各位高手帮帮小弟!!
f:\tufu\tree\treenode.h(29) : error C2953: 'TwoTreeNode' : template class has already been defined
f:\tufu\tree\treenode.h(29) : see declaration of 'TwoTreeNode'
f:\tufu\tree\treenode.h(39) : error C2995: 'GetTreeNode' : template function has already been defined
f:\tufu\tree\treenode.h(28) : see declaration of 'GetTreeNode'
Error executing cl.exe.
Tree.exe - 2 error(s), 0 warning(s)
代码如下:
template <class T>
class TwoTreeNode
{
public:
T data;
TwoTreeNode<T> * leftchild, * rightchild;
TwoTreeNode(void) {leftchild=rightchild=NULL;}
TwoTreeNode(const T& item)
{
data = item;
leftchild = rightchild = NULL;
}
TwoTreeNode(const T& item,TwoTreeNode *left,TwoTreeNode *right)
{
data = item;
leftchild = left;
rightchild = right;
}
void FreeTreeNode(TwoTreeNode<T> * p){delete p;}
TwoTreeNode<T> * GetTreeNode(T item,TwoTreeNode<T> * left=NULL,TwoTreeNode<T> * right=NULL);
};
template <class T>
TwoTreeNode<T> * TwoTreeNode<T>::GetTreeNode(T item,TwoTreeNode<T> * left,TwoTreeNode<T> * right)
{
TwoTreeNode<T> * p;
p = new TwoTreeNode<T>(item,left,right);
if(p==NULL)
cerr << "Memory allocation failure! \n";
return p;
}
请各位高手帮帮小弟!!