我正在构建一个c++程序,我收到这个错误。我也收到2个类似的事情错误。
我知道这通常是由于没有链接库或其他性质的东西,但我目前没有使用库?至少我不这么认为。
error LNK2019: unresolved external symbol "public: void__thiscall
Start::showMenu(void)"(?showMenu@start""QAEXXZ) referenced in function
"public: __thiscall Start:Start(void)" (??Start@@AE@XZ)
这里是我的错误和代码。如有任何帮助,不胜感激。
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 SystemnPlease 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.nn";
return 1;
}else{
if(chosenIndex>endCounter || chosenIndex<endCounter){
cout << "You have chosen an invalid option. Please try again.nn";
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. nn";
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:
};
您在Start.cpp
的命名空间范围内声明和定义了以下内容,而您可能并不想这样做:
void bookNew();
void viewReceipts();
void showMenu();
你的构造函数正在调用Start::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 Systemn"
"Please choose an action from the list below "
"by typing the corresponding number.n";
// ...
}
void Start::bookNew() { /* ... */ }
void Start::viewReceipts() { /* ... */ }
您也可能在Routes
中遇到类似的问题,它使用名称空间范围的方法和全局变量,而不是类的等效成员。