2012-05-18 51 views
1

當我向我的BST實現中添加Retreive()方法時,出現「未解析的外部」編譯器錯誤。 Compile Error在二進制搜索樹中檢查重複值的方法導致無法解析的外部結果

Retrieve()成員函數檢查用戶輸入的值是否存在於BST中,如果它確實設置了found = false。

//Header File 
using namespace std; 

struct PersonRec 
{ 
    char name[20]; 
    int bribe; 
    PersonRec* leftLink; 
    PersonRec* rightLink; 
}; 


class CTree 
{ 

private: 
    PersonRec *tree; 
    bool IsEmpty(); 
    void AddItem(PersonRec*&, PersonRec*); 
    void DisplayTree(PersonRec*); 
    void Retrieve(PersonRec*, PersonRec*,bool&); 

public: 
    CTree(); 
    //~CTree(); 
    void Add(); 
    void View(); 

}; 




//Implementation File 
#include <iostream> 
#include <string> 

using namespace std; 

#include "ctree.h" 

CTree::CTree() 
{ 
    tree = NULL; 
} 

//PersonList::~MyTree() 
//{ 
// 
//} 


bool CTree::IsEmpty() 
{ 
    if(tree == NULL) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

void CTree::Add() 
{ 
    PersonRec* newPerson = new PersonRec(); 
    cout << "Enter the person's name: "; 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    cin.getline(newPerson->name, 20); 
    cout << "Enter the person's contribution: "; 
    cin >> newPerson->bribe; 
    bool found = false; 


    newPerson->leftLink = NULL; 
    newPerson->rightLink = NULL; 

    Retrieve(tree, newPerson, found); 
    if (found) 
     cout << "Bribe allready entered\n"; 
    else 
     AddItem(tree, newPerson); 
} 

void CTree::View() 
{ 
    if (IsEmpty()) 
    { 
     cout<<"The list is empy"; 
    } 
    else 
    { 
     DisplayTree(tree); 

    } 

}; 

void CTree::AddItem(PersonRec*& ptr, PersonRec* newPer) 
{ 
     if (ptr == NULL) 
     { 
      ptr = newPer; 
     } 
     else if (newPer->bribe < ptr->bribe) 
      AddItem(ptr->leftLink, newPer); 
     else 
      AddItem(ptr->rightLink, newPer); 
} 
void CTree::DisplayTree(PersonRec* ptr) 
{ 
    if (ptr == NULL) 
        return; 
    DisplayTree(ptr->rightLink); 
    cout<<ptr->name<<" "<<"$"<<ptr->bribe <<endl; 
    DisplayTree(ptr->leftLink); 
} 
void Retrieve(PersonRec*& ptr, PersonRec* newPer, bool& found) 
{ 
    { 
     if (ptr == NULL) 
     { 
      found = false; // item is not found. 
     } 
     else if (newPer->bribe < ptr->bribe) 
     { 
      Retrieve(ptr->leftLink, newPer, found); 
     } 
      // Search left subtree. 
     else if (newPer->bribe > ptr->bribe) 
     { 
      Retrieve(ptr->rightLink, newPer, found);// Search right subtree. 
     } 
     else 
     { 
      //newPer.bribe = ptr->info; // item is found. 
      found = true; 
     } 
    } 
} 
//Driver File 
#include <iostream> 

using namespace std; 
#include <cstdlib> 
#include "ctree.h" 

int displayMenu (void); 
void processChoice(int, CTree&); 

int main (void) 
{ 
int num; 
CTree ct; 
do 
{ 
num = displayMenu(); 
if (num != 3) 
processChoice(num, ct); 
} while (num != 3); 
return 0; 
} 

int displayMenu (void) 
{ 
int choice; 
cout << "\nMenu\n"; 
cout << "==============================\n\n"; 
cout << "1. Add student to waiting list\n"; 
cout << "2. View waiting list\n"; 
cout << "3. Exit program\n\n"; 
cout << "Please enter choice: "; 
cin >> choice; 
return choice; 
} 

void processChoice(int choice, CTree& myTree) 
{ 
    switch (choice) 
    { 
     case 1: myTree.Add(); break; 
     case 2: myTree.View(); break; 
    } 
} 
+0

你就不能複製的錯誤?檢查輸出標籤... –

回答

3

你沒有資格與類名的定義:

void Retrieve(PersonRec*& ptr, PersonRec* newPer, bool& found) 

應該

void CTree::Retrieve(PersonRec* ptr, PersonRec* newPer, bool& found) 
+0

加上第一個參數是不同的 –

+0

感謝您的幫助!是的,我意識到在我的聲明中,我讓PersonRec成爲一個指針,但忘記了將它作爲參考。 – Zzz

+0

@ K-ballo,很好的電話,固定。 –