在开始之前,我只想先写一下这是我在stackoverflow上的第一篇文章。
我正在为类的二叉搜索树工作。我有一个插入函数完成,以及打印节点的遍历。但是,我收到一个错误,阻止我编译测试我的树。这是我的.h文件,它是一个模板,所以一切都包含在这里。
我收到LNK错误,而且从我所读过的内容来看,我仍然无法理解为什么会这样。
错误3错误LNK2019:未解析的外部符号" private:void __thiscall main_dc :: BST :: helperInOrder(struct main_dc :: Node *&)"函数" public:void __thiscall main_dc :: BST :: traversalInOrder(void)"(?helperInOrder @?$ BST @H @ main_dc @@ AAEXAAPAU?$ Node @ H @ 2 @@ Z) (?traversalInOrder @?$ BST @H @ main_dc @@ QAEXXZ)\ codingpc \ Users \ Devyn \ Documents \ Visual Studio 2013 \ Projects \ 2420_Data_And_Algorithm \ BinarySearchTree_Project_07 \ BinarySearchTree_Project_07 \ Driver.obj BinarySearchTree_Project_07
template <class T>
class BST
{
public:
BST();
~BST(); // delete root that deletes left and right child
int getSize();
void print();
void erase(T item); // item is an integer number
void insert(T item);
// Traversal functions (pre-order, in-order, post-order)
// void traverselPreOrder();
void traversalInOrder();
void traversalPostOrder();
private:
// Helper function for insert
void insertHelper(Node<T>*& n, T item);
//Traversal helper functions
void helperPostOrder(Node<T>*& n);
void helperInOrder(Node<T>*& n);
// member data
Node<T>* root;
int size;
};
template <class T>
BST<T>::BST()
{
root = nullptr;
size = 0;
}
template <class T>
BST<T>::~BST()
{
// delete root;
}
template <class T>
int BST<T>::getSize()
{
// return size;
// return number of elements in the tree
}
template <class T>
void BST<T>::print()
{
// pre order traversal
// print one element per line
// indent according to the depth (see page 505)
}
template <class T>
void BST<T>::traversalInOrder()
{
helperInOrder(root);
}
template <class T>
void helperInOrder(Node<T>*& nPtr)
{
// something in tree - if root != null
if (root != nullptr)
{
// if root != null... if left child pointer != null, recursively call helper, passing ptrs left
if (nPtr->lchild != nullptr)
{
helperInOrder(nPtr->lchild);
}
// print out current node
cout << nPtr->data;
if (nPtr->rchild != nullptr)// if right child pointer != null, recursively call helper, passing ptrs right
{
helperInOrder(nPtr->rchild)
}
}
else // else tree is empty print out tree is empty
{
cout << "The tree is empty" << endl;
}
}
template <class T>
void BST<T>::erase(T item)
{
// erase the occurrence of the item in the tree (there should only be 1
// or 0 because there are no dupes)
// resulting tree should still be a binary search tree (balanced AND full?)
}
template <class T>
void BST<T>::insert(T item)
{
insertHelper(root, item);
}
template <class T>
void BST<T>::insertHelper(Node<T>*& nPtr, T item) // insert an item into BTS, do not allow duplicate numbers
{
if (root == nullptr) // if nothing in tree - insert node
{
root = new Node<T>(item);
}
// if something in tree - check to see if less or greater than node you are on
else if (item < nPtr->data) // if item is less than data in node and nothing is in lchild, create a node
{
if (nPtr->lchild != nullptr) // if left child has a value, traverse left once
{
insertHelper(nPtr->lchild, item);
}
else
{
nPtr->lchild = new Node<T>(item);
}
}
else if (item > nPtr->data) // if item is greater than data node and nothing is in rchild, create a node
{
if (nPtr->rchild != nullptr) // if right child already exsists, traverse right once
{
insertHelper(nPtr->rchild, item);
}
else
{
nPtr->rchild = new Node<T>(item);
}
}
else // parent has no place for item to go
{
cout << "The input " << item << " has already been added to the tree." << endl;
}
}
}
答案 0 :(得分:1)
缺少类范围 void helperInOrder(Node *&amp; nPtr)