template <class T>
class BTree
{
private:
T Data;
BTree* Right;
BTree* Left;
public:
BTree();
BTree(T);
~BTree();
void SetData(T);
T GetData();
void SetRight(BTree*);
BTree* GetRight();
void SetLeft(BTree*);
BTree* GetLeft();
};
这是声明非成员函数模板的方式吗?我对这些定义非常怀疑。
template <class T>
BTree<T>* NewNode();
template <class T>
BTree<T>* NewNode(T);
这下面不起作用!!给我一个错误
template <class T>
BTree<T>* NewNode()
{
BTree<T>* Node=new BTree<T>;
return Node;
}
template <class T=int>
//template <class BTree>
BTree<T>* NewNode(T Num)
{
BTree<T>* Node=new BTree<T>(Num);
return Node;
}
int main()
{
BTree<int>* Root=NULL;
Root=NewNode(1); //it says undefined reference.
Root->SetLeft(NewNode(2));
Root->SetRight(NewNode(3));
return 0;
}
错误如下:
1.undefined reference to `BTree<int>* NewNode<int>(int)'
2.undefined reference to `BTree<int>::SetLeft(BTree<int>*)'
3.undefined reference to `BTree<int>::SetRight(BTree<int>*)'