error LNK2019: unresolved external symbol "public: void__thiscall Start::showMenu(void)" [duplicate]

StackOverflow https://stackoverflow.com/questions/23686142

Pregunta

Im building a c++ program and i am recieving this error. I am also receiving 2 more errors for similar things.

I understand that this is normally due to not linking a library or something of that natrue, but im not currently using a library? atleast i dont think i am.

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)

here is my error and code. Any help would be appreciated.

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;
}

Routes

#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: 

};
¿Fue útil?

Solución

You are declaring and defining the following at namespace scope in Start.cpp when you probably don't mean to:

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

Your constructor is calling Start::showMenu(), which you actually did declare properly:

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

You need to get rid of the three declarations above and redefine the implementations as follows:

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() { /* ... */ }

You may also have similar problems with Routes, which is using namespace-scope methods and global variables instead of class member equivalents.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top