我在尝试将节点插入二叉搜索树时遇到问题。我不断得到
错误 LNK2019:未解析的外部符号“public:__thiscall tree::tree(void)”(??0?$tree@H@@QAE@XZ)
有人可以告诉我问题是什么。
树节点.h
template <class T> class tree;
//treenode class-template definition
template <class T>
class treenode
{
friend class tree<T>;
public:
treenode(const T &d, treenode<T> *l, treenode<T> *r)
: data(d), leftptr(l), rightptr(r)
{
}
treenode(const T &d):
leftptr(0),
data(d),
rightptr(0)
{
}
T getData() const
{
return data;
}
private:
treenode<T> *leftptr;
T data;
treenode<T> *rightptr;
};//end class treenode
树.h
class tree
{
public:
tree(); //initializes the private data member
void insertNode (treenode<T> **, const T &);
void preOrderTraversal(treenode<T> * ) const;
void postOrderTraversal(treenode<T> * ) const;
void inorderTraversal(treenode<T> *) const;
private:
treenode<T> *rootptr;
};
树.cpp
#include <iostream>
#include "tree.h"
using namespace std;
//constructor
template< class T>
tree<T>::tree()
{
rootptr = 0; //indicate tree is initially empty
}//end tree constructor
//insert node in tree
template <class T>
void tree<T>::insertNode(treenode<T> **rootptr, const T &val)
{
// subtree is empty; create new treenode containing value
if(*rootptr == 0)
*rootptr = new treenode<T>(val);
else // subtree is not empty
{
// data to insert is less than data in current node
if(val < (*rootptr)->data)
{
insertNode(&((*rootptr)->leftptr), val);
}
else
{
// data to insert is greater than data in current node
if(val > (*rootptr)->data)
{
insertNode(&((*rootptr)->rightptr), val);
}
}//end else
}//end else
}//end function insertNode
测试器.cpp
int main()
{
tree <int> create;
int userVal;
cout << "Enter 10 Integers" << "\n\n";
for(int i=0; i<10; i++)
{
cout << "-> ";
cin >> userVal;
createTree.insertNode(0, userVal);
}
system("pause");
return 0;
}//end main