2013-05-12 60 views
0

我正在爲二叉搜索樹的模板工作,我得到下面的錯誤。我知道有類似的帖子,但我無法弄清楚這件事。模板對我來說絕對是一個薄弱的主題,所以我認爲我錯過了一些基本的語義上的東西,但我可以把它放在手指上。下面是錯誤,然後是程序的文件。如果任何人都可以指出我正確的方向,我會非常感激。模板化的二叉搜索樹C++,無法解析的外部錯誤

錯誤1個錯誤LNK2019:無法解析的外部符號 「私人:BOOL __thiscall樹:: insertHelper(類樹節點* &,INT)」(insertHelper @ $樹3 H @@ AAE_NAAPAV $ @樹節點H +??? @ H @ Z)在函數「public:bool __thiscall Tree :: insert(int)」(?insert @?$ Tree @ H @@ QAE_NH @ Z)中引用C:\ Users \ wm \ documents \ visual studio 2012 \ Projects \二叉樹\二叉樹\ main.obj

- 基本主要用於測試

#include "BinarySearchTree.h" 
#include "TreeNode.h" 
#include <iostream> 

using namespace std; 

int main() 
{ 
    Tree<int> test; 
    test.insert(55); 

    return 0; 
} 

- 樹模板

#include <iostream> 
#include "TreeNode.h" 

using namespace std; 

template< class ItemType > 
class Tree { 
public: 

    Tree(); 
    ~Tree();  
    bool isEmpty() const; 
    void makeEmpty(); 
    bool insert(ItemType newItem); 
    bool retrieve(ItemType searchItem, ItemType & foundItem); 
    bool deleteItem (ItemType deleteItem); 
    void print(); 

private: 

    TreeNode<ItemType> * rootPtr; // pointer to the root 

    //utility functions 
    void printHelper(TreeNode<ItemType> *); 
    bool insertHelper(TreeNode<ItemType> * & node, ItemType item); 
    bool deleteHelper(TreeNode<ItemType> * & , ItemType); 
    void deleteNode(TreeNode<ItemType > * &); 
    bool retrieveHelper(ItemType, TreeNode<ItemType> * & , ItemType &); 

}; 

template<class ItemType> 
Tree<ItemType>::Tree() 
{ 
    rootPtr = NULL; 
} 
template<class ItemType> 
Tree<ItemType>::~Tree() 
{ 
    makeEmpty(); 
} 
template<class ItemType> 
bool Tree<ItemType>::isEmpty() const 
//Returns True if the tree is empty, otherwise returns false 
//Postcondition: Tree is unchanged. 
{ 
    if(rootPtr == NULL) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
template<class ItemType> 
void Tree<ItemType>::makeEmpty() 
//Makes the tree empty if it is not empty already. 
//Preconditions: The tree exists. 
//Postconditions: Tree is now empty. Any dynamically allocated memory which is no longer used is returned to the system. 
{ 
    return; 
} 
template<class ItemType> 
bool Tree<ItemType>::insert(ItemType newItem) 
// Inserts a copy of newItem in the tree. 
//Precondition: The tree exists and has binary search property. 
//Postcondition: if the tree already has an item where item == newItem, the function returns false and the trre is unchanged. 
//Otherwise, the newItem is inserted in the tree preserving and the binary search property is maintained. 
{ 
    if(rootPtr == NULL) 
    { 
     rootPtr->data = newItem; 

     return true; 
    } 
    else 
    { 
     return insertHelper(rootPtr, newItem); 
    } 
} 
template<class ItemType> 
bool insertHelper(TreeNode<ItemType> * & node, ItemType item) 
{ 
    if(item < node->data)//left 
    { 
     if(node->leftPtr == NULL) 
     { 
      node->leftPtr = new TreeNode<ItemType>(item); 
      return true; 
     } 
     else 
     { 
      return insertHelper(node->leftPtr,item); 
     } 
    } 
    else if(node->data < item)//right 
    { 
     if(node->righChild == NULL) 
     { 
      node->rightPtr = new TreeNode<ItemType>(item); 
      return true; 
     } 
     else 
     { 
      return insertHelper(node->rightPtr,item); 
     } 
    } 
    else// same value 
    { 
     return false; 
    } 
} 
template<class ItemType> 
bool Tree<ItemType>::retrieve(ItemType searchItem, ItemType & foundItem) 
// Given a searchItem, it tries to retrieve as foundItem, an item in the tree where the item == searchItem. 
// Precondition:The tree exists and has the binary search property. 
// Postcondition: If the tree already has an item where item == searchItem, foundItem is set to that item, and the function returns true. 
// If the tree has no such item, the function returns false and foundItem remains unchanged. The tree is unchanged. 
{ 

} 
template<class ItemType> 
bool Tree<ItemType>::deleteItem (ItemType deleteItem) 
// Given a deleteItem, it deltes from the tree any item where item == deleteItem. 
// Precondition: Tree exists and has binary search property. 
// Postcondition: If the tree has an item where item == deleteItem, that item is removed from the tree, and the function returns true, and 
// the binary search property is maintained. If the tree has no such item, the function returns false and the tree remains unchanged. 
{ 

} 
template<class ItemType> 
void Tree<ItemType>::print() 
// Prints the items in the tree in ascending order to the screen 
// Precondition: The tree exists and has binary search property. 
// Postcondition: The items have been printed in ascending order and the tree is unchanged 
{ 

} 
template<class ItemType> 
void printHelper(TreeNode<ItemType> *) 
{ 

} 
template<class ItemType> 
bool deleteHelper(TreeNode<ItemType> * & , ItemType) 
{ 

} 
template<class ItemType> 
void deleteNode(TreeNode<ItemType > * &) 
{ 

} 
template<class ItemType> 
bool retrieveHelper(ItemType, TreeNode<ItemType> * & , ItemType &) 
{ 

} 

-treenode

#ifndef TREENODE_H 
#define TREENODE_H 

template< class ItemType > class Tree; // forward declarations 

template<class ItemType> 
class TreeNode { 
    friend class Tree<ItemType>; // make Tree a friend 

public: 
    TreeNode(ItemType); // constructor 
    TreeNode();     // constructor with data uninitialized 
    ItemType getData() const;  // return data in the node 
private: 
    ItemType data;     
    TreeNode<ItemType> *leftPtr; 
    TreeNode<ItemType> *rightPtr; 
}; 

// Constructor 
template<class ItemType> 
TreeNode<ItemType>::TreeNode(ItemType newItem) 
{ 
    data = newItem; 
    leftPtr = NULL; 
    rightPtr = NULL; 
} 

template<class ItemType> 
TreeNode<ItemType>::TreeNode() 
{ 
    leftPtr = NULL; 
    rightPtr = NULL; 
} 


// Return a copy of the data in the node 
template< class ItemType > 
ItemType TreeNode<ItemType>::getData() const 
{ 
    return data; 
} 

#endif 
+0

值得注意的是,std :: set與樹具有相同的屬性。在插入/查找/刪除方面 – 2013-05-12 05:47:42

+0

PS:當你得到它的工作。您可能需要完整的代碼審查。看到http://codereview.stackexchange.com/ – 2013-05-12 06:05:05

回答

2

嘿。問題在於你忘記了在函數名稱前爲insertHelper的範圍限定符樹<ItemType> ::

只是這樣做:

template<class ItemType> 
bool Tree<ItemType>::insertHelper(TreeNode<ItemType> * & node, ItemType item) 

順便說一句,鏈接器錯誤應該向你建議,這就是問題所在。首先,你知道你有一個鏈接問題,而不是編譯問題。其次,您知道無法找到的符號是* 樹<ItemType> :: * insertHelper(...)。第三,您知道對該函數的調用已成功編譯爲樹<ItemType> :: insert,這意味着編譯器發現並解析了聲明,insertHelper方法沒有問題。鏈接器無法找到該方法的實際目標代碼的唯一可能的解釋是該函數的定義不存在,或者與您聲明的名稱不同!

1

這裏:

if(rootPtr == NULL) 
{ 
    rootPtr->data = newItem; 

    return true; 
} 

如果rootPtr是NULL然後rootPtr->data無效。

也許你的意思是:

if(rootPtr == NULL) 
{ 
    rootPtr = new TreeNode<ItemType>(newItem); 
} 
+0

是的,我剛剛發現。 – wmurmann 2013-05-12 06:23:19