2014-05-15 56 views
-1

我建立一個C++程序,我收到這個錯誤。對於類似的事情,我也收到2個更多的錯誤。錯誤LNK2019:無法解析的外部符號「public:void__thiscall Start :: showMenu(void)」

據我所知,這通常是由於沒有鏈接一個庫或那個natrue的東西,但我目前沒有使用庫?至少我不認爲我是。

error LNK2019: unresolved external symbol "public: void__thiscall 
Start::showMenu(void)"([email protected]""QAEXXZ) referenced in function 
"public: __thiscall Start:Start(void)" ([email protected]@[email protected]) 

這是我的錯誤和代碼。任何幫助,將不勝感激。

Start.cpp

#include "Start.h" 
#include "Routes.h" 
#include "std_lib_facilities.h" 
using namespace std; 


void bookNew(); 
void viewReceipts(); 
void showMenu(); 


Start::Start(void) 
{ 
    showMenu(); 
} 


Start::~Start(void) 
{ 
} 

//Function Definitions 
void showMenu(){ 
     //output to the user 
    cout << "Welcome to the Flight Booking System\nPlease choose an action from the  list below by typing the corresponding number.\n"; 
     //array of choices that need to be output for the user 
    string top_level_options[] = {"[1]->View Receipts","[2]->Book new flight","[3]->Exit","Please input your choice:\n"}; 
//loops through choices and displays to the user 
for each (string i in top_level_options) { 
    cout << i + "\n"; 
} 

int input; 
//reads the users input 
cin >> input; 
//does action based on the input from the user 
switch (input-1) { 
case 0 : 
    viewReceipts(); 
    break; 
case 1 : 

    bookNew(); 
    break; 
case 2 : 
    exit(1); 
    break; 
default : cout << "You entered an invalid option. Please try again."; 
    showMenu(); 
    break; 

} 
showMenu(); 
system("PAUSE");  
} 

void bookNew(){ 
//new instance of the Route class 
cout << "You chose to book a new flight: \n"; 
Routes newJourney; 

switch (newJourney.setStart()) { 
case 1 : 
    showMenu(); 
    break; 
case 2 : 
    cout << "You have entered an invalid option. Please try again."; 
    newJourney.setStart(); 
    break; 
case 3 : 
    switch (newJourney.setDestination()) { 
    case 1 : 
     newJourney.setStart(); 
     break; 
    case 2 : 
     cout << "You have entered an invalid option. Please try again."; 
     newJourney.setDestination(); 
     break; 
    case 3 : 
     cout << "You have chosen to travel from"; 
     break; 
    } 
    break; 
} 
} 

void viewReceipts(){ 
cout << "You have chosen to view exisiting bookings.\nPlease choose a booking you  would like to view by typing the correspoding number and hitting enter.\n"; 
} 

start.h

#pragma once 
class Start 
{ 
public: 
Start(void); 
~Start(void); 
void bookNew(); 
void viewReceipts(); 
void showMenu(); 
}; 

的main.cpp

#include "std_lib_facilities.h" 
#include "Routes.h" 
#include "Start.h" 
using namespace std; 
//Function Declarations 


//Main Function 
int main() 
{ 
Start menu; 

return 0; 
} 

路線

#include "Routes.h" 
#include "std_lib_facilities.h" 
#include "Start.h" 
using namespace std; 


vector<vector<string>> airports; 
int startLocation; 
int endLocation; 

//function declarations 
void readInput(); 
int setStart(); 
int setDestination(); 
void getDetails(); 

Routes::Routes(void) 
{ 
//reads in the file and populates the arrays 
readInput(); 



} 


Routes::~Routes(void) 
{ 
} 






void readInput() 
{ 
//opens the file 
ifstream inFile; 
inFile.open("data.txt"); 
//checks that the file reading in did not fail 
if(inFile.fail()){ 
    cerr << ("cant open data file."); 
} 
//variable to store the current word that is being analysed 

//while the file isnt at the end 
while(!inFile.eof()){ 
    string currentWord; 
    //vector to store the routes  
    vector<string> currentStops; 
    //loops through every word in the file 
    while(inFile >> currentWord){ 
     //if it reaches a ! ---end of the line 
     if(currentWord=="!"){ 
      //adds the current vector to the airport vector 
      airports.push_back(currentStops); 
      //clears the current vector 
      currentStops.clear(); 
     }else{ 
      //adds the current word to the current vector 
      currentStops.push_back(currentWord);  
     } 
    } 
} 
} 



int setStart() 
{ 
cout << "Please select a start location from the list below by entering the corresponding number and hitting enter." << endl; 
int endCounter = 0; 
cout << "\n"; 
for(unsigned int i=0;i<airports.size();i++){ 
    cout << "[" << i+1 << "] " << airports[i][0] << endl; 
    endCounter = i+1; 
} 
cout << "[" << endCounter << "] Go Back" << "\n"; 

int chosenIndex; 
cin >> chosenIndex; 
if(chosenIndex==endCounter){ 
    cout << "You have chosen to go back.\n\n"; 
    return 1; 

}else{ 
    if(chosenIndex>endCounter || chosenIndex<endCounter){ 
     cout << "You have chosen an invalid option. Please try again.\n\n"; 
     setStart(); 
     return 2; 
    }else{ 
     startLocation = chosenIndex-1; 
     return 3; 
    } 
} 
return 0; 
} 

int setDestination() 
{ 
cout << "Please choose your desired destination: \n"; 
cout << "From " << airports[startLocation][0] << " to: \n"; 
int endCounter; 
for(unsigned int i=1;i<airports[startLocation].size();i++){ 
    cout << "[" << i << "] " << airports[startLocation][i] << endl; 
    endCounter = i+1; 
} 

cout << "[" << endCounter << "] Go Back" << "\n"; 

int chosenIndex; 
cin >> chosenIndex; 
if(chosenIndex==endCounter){ 
    cout << "You chose to go back. \n\n"; 
    return 1; 
}else{ 
    if(chosenIndex>endCounter){ 
     cout << "You chose an invalid option. Please try again."; 
     return 2; 
    }else{ 
     endLocation = chosenIndex -1; 
     return 3; 
    } 
} 
return 0; 
} 

void getDetails() 
{ 
cout << "You have chosen to travel from " << airports[startLocation][0] << " to " <<  airports[startLocation][endLocation] << ".\n"; 
} 

Routes.h

#pragma once 
class Routes 
{ 
public: 
Routes(void); 
~Routes(void); 
int setStart(); 
int setDestination(); 
private: 

}; 
+1

在'start.h'中,你聲明瞭一個'Start :: ShowMenu()'成員方法,你可以在'Start :: Start()'中調用,而不是命名空間範圍'void showMenu()'認爲你在打電話。將調用改爲':: showMenu()'等等,從類定義中刪除聲明,或者(很可能是你想要的)從Start.cpp中移除三個名稱空間範圍的前向聲明並更改定義那裏'無效Start :: showMenu(){/*...*/}'等 – Jeff

回答

0

您在Start.cpp聲明和定義在命名空間範圍以下時,你可能並不想:

void bookNew(); 
void viewReceipts(); 
void showMenu(); 

你的構造呼籲啓動:: showMenu()你實際上並正確聲明:

Start::Start(void) { 
    showMenu(); // this calls Start::showMenu(), which is not what you defined. 
} 

你需要擺脫三個聲明以上的和重新定義的實現如下:

void Start::showMenu() { 
    //output to the user 
    cout << "Welcome to the Flight Booking System\n" 
    "Please choose an action from the list below " 
    "by typing the corresponding number.\n"; 
    // ... 
} 

void Start::bookNew() { /* ... */ } 

void Start::viewReceipts() { /* ... */ } 

您可能也會遇到Routes類似的問題,它使用命名空間範圍方法和全局變量而不是類成員等效方法。

+0

非常完美謝謝你。第一次編碼C++因此我的不高興 – mwild

+1

很高興我能幫上忙。只要簡單介紹成員和命名空間範圍方法和變量之間的區別,並記住類定義主體之外的成員定義所需的'ClassName :: member'語法。 – Jeff

相關問題