2014-05-15 126 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

相关问题