0
我無法嘗試將節點插入到二進制搜索樹中。我不斷收到使用cpp將節點插入到二進制搜索樹中
錯誤LNK2019:無法解析的外部符號 「公用:__thiscall樹::樹(無效)」(??? 0 $樹3 H @@ QAE @ XZ)
有人能告訴我是什麼問題是請。
treenode.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
tree.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;
};
tree.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
tester.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
您在類「樹」定義之前缺少'template'。我認爲這是你的問題中的一個簡單的遺漏,但我發信號以防萬一。 –
didierc