Sign in to follow this  

C++ airport program - Help!

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am learning to program with C++ at the moment and I found the following program in a "teach youself" book which is an airport simulation. The program is made up of three files here called Aiport.cpp, Airplane.cpp and Airplane.h. When I run the program I get the following three error messages - "[Linker Error] Unresolved external 'Airplane::~Airplane()' referenced from D:\DOCUMENTS AND SETTINGS\MARK DONALDSON\DESKTOP\TYBCB21\DAY04\AIRPORT.OBJ" "[Linker Error] Unresolved external 'Airplane::Airplane(const char *, int)' referenced from D:\DOCUMENTS AND SETTINGS\MARK DONALDSON\DESKTOP\TYBCB21\DAY04\AIRPORT.OBJ" "[Linker Error] Unresolved external 'Airplane::SendMessageA(int, char *, int, int, int)' referenced from D:\DOCUMENTS AND SETTINGS\MARK DONALDSON\DESKTOP\TYBCB21\DAY04\AIRPORT.OBJ" Is there an error in the program or is it something else?What do I have to change to get the program to work? I am only learning at the moment and I need this program to be working in the first place in order to learn how it works. The three files are listed below. This file is called Airport.cpp //--------------------------------------------------------------------------- #include <vcl\condefs.h> #include <iostream.h> #include <conio.h> #pragma hdrstop //--------------------------------------------------------------------------- USERES("Airport.res"); USEUNIT("airplane.cpp"); //--------------------------------------------------------------------- #include "airplane.h" int getInput(int max); void getItems(int& speed, int& dir, int& alt); int main(int argc, char **argv) { char returnMsg[100]; // // Set up an array of Airplanes and create // three Airplane objects. // Airplane* planes[3]; planes[0] = new Airplane("TWA 1040"); planes[1] = new Airplane("United Express 749", COMMUTER); planes[2] = new Airplane("Cessna 3238T", PRIVATE); // // Start the loop. // do { int plane, message, speed, altitude, direction; speed = altitude = direction = -1; // // Get a plane to whom a message will be sent. // List all of the planes and let the user pick one. // cout << endl << "Who do you want to send a message to?"; cout << endl << endl << "0. Quit" << endl; for (int i=0;i<3;i++) cout << i + 1 << ". " << planes[i]->name << endl; // // Call the getInput() function to get the plane number. // plane = getInput(4); // // If the user chose item 0 then break out of the loop. // if (plane == -1) break; // // The plane acknowledges. // cout << endl << planes[plane]->name << ", roger."; cout << endl << endl; // // Allow the user to choose a message to send. // cout << "What message do you want to send?" << endl; cout << endl << "0. Quit" << endl;; cout << "1. State Change" << endl; cout << "2. Take Off" << endl; cout << "3. Land" << endl; cout << "4. Report Status" << endl; message = getInput(5); // // Break out of the loop if the user chose 0. // if (message == -1) break; // // If the user chose item 1 then we need to get input // for the new speed, direction, and altitude. Call // the getItems() function to do that. // if (message == 0) getItems(speed, direction, altitude); // // Send the plane the message. // bool goodMsg = planes[plane]->SendMessage( message, returnMsg, speed, direction, altitude); // // Something was wrong with the message // if (!goodMsg) cout << endl << "Unable to comply."; // // Display the plane's response. // cout << endl << returnMsg << endl; } while (1); // // Delete the Airplaine objects. // for (int i=0;i<3;i++) delete planes[i]; } int getInput(int max) { int choice; do { choice = getch(); choice -= 49; } while (choice < -1 || choice > max); return choice; } void getItems(int& speed, int& dir, int& alt) { cout << endl << "Enter new speed: "; getch(); cin >> speed; cout << "Enter new heading: "; cin >> dir; cout << "Enter new altitude: "; cin >> alt; cout << endl; } This file is called Airplane.cpp #include <iostream.h> #include <stdio.h> #pragma hdrstop #include "airplane.h" // // Constructor performs initialization // Airplane::Airplane(const char* _name, int _type) : type(_type), status(ONRAMP), speed(0), altitude(0), heading(0) { switch (type) { case AIRLINER : ceiling = 35000; break; case COMMUTER : ceiling = 20000; break; case PRIVATE : ceiling = 8000; } name = new char[50]; strcpy(name, _name); } // // Destructor performs cleanup. // Airplane::~Airplane() { delete[] name; } // // Gets a message from the user. // bool Airplane::SendMessage(int msg, char* response, int spd, int dir, int alt) { // // Check for bad commands. // if (spd > 500) { strcpy(response, "Speed cannot be more than 500."); return false; } if (dir > 360) { strcpy(response, "Heading cannot be over 360 degrees."); return false; } if (alt < 100 && alt != -1) { strcpy(response, "I'd crash, bonehead!"); return false; } if (alt > ceiling) { strcpy(response, "I can't go that high."); return false; } // // Do something base on which command was sent. // switch (msg) { case MSG_TAKEOFF : { // Can't take off if already in the air! if (status != ONRAMP) { strcpy(response, "I'm already in the air!"); return false; } TakeOff(dir); break; } case MSG_CHANGE : { // Can't change anything if on the ground. if (status == ONRAMP) { strcpy(response, "I'm on the ground"); return false; } // Only change if a non-negative value was passed. if (spd != -1) speed = spd; if (dir != -1) heading = dir; if (alt != -1) altitude = alt; status == CRUISING; break; } case MSG_LAND : { if (status == ONRAMP) { strcpy(response, "I'm already on the ground."); return false; } Land(); break; } case MSG_REPORT : ReportStatus(); } // // Standard reponse if all went well. // strcpy(response, "Roger."); return true; } // // Perform takeoff. // void Airplane::TakeOff(int dir) { heading = dir; status = TAKINGOFF; } // // Perform landing. // void Airplane::Land() { speed = heading = altitude = 0; status == ONRAMP; } // // Build a string to report the airplane's status. // int Airplane::GetStatus(char* statusString) { sprintf(statusString, "%s, Altitude: %d, Heading: %d, " "Speed: %d\n", name, altitude, heading, speed); return status; } // // Get the status string and output it to the screen. // void Airplane::ReportStatus() { char buff[100]; GetStatus(buff); cout << endl << buff << endl; } This file is called airplane.h //--------------------------------------------------------------------------- #ifndef airplaneH #define airplaneH //--------------------------------------------------------------------------- #define AIRLINER 0 #define COMMUTER 1 #define PRIVATE 2 #define TAKINGOFF 0 #define CRUISING 1 #define LANDING 2 #define ONRAMP 3 #define MSG_CHANGE 0 #define MSG_TAKEOFF 1 #define MSG_LAND 2 #define MSG_REPORT 3 class Airplane { public: Airplane(const char* _name, int _type = AIRLINER); ~Airplane(); virtual int GetStatus(char* statusString); int GetStatus() { return status; } int Speed() { return speed; } int Heading() { return heading; } int Altitude() { return altitude; } void ReportStatus(); bool SendMessage(int msg, char* response, int spd = -1, int dir = -1, int alt = -1); char* name; protected: virtual void TakeOff(int dir); virtual void Land(); private: int speed; int altitude; int heading; int status; int type; int ceiling; }; #endif Any help would be much appreciated. Thanks Mark

Share this post


Link to post
Share on other sites
Your question belongs in For Beginners.

Linker errors are a different class of error from compiler errors. Normally, you can find a solution for them in here. However, in your case it looks like the compiler simply hasn't been properly informed about which files are part of the project.

That said - throw the book away and burn it. The sample code provided here is spoiling my lunch hour. It's doing a *lot* of things wrong. They may compile, but the code is years out of date with the standard (iostream.h is deprecated, non-standard and just plain ancient, and different compilers take different approaches to bending over backwards to support it), and completely fails to take advantage of the modern C++ standard library (which makes it a lot easier to do common things "safely", i.e. without risk of creating undefined behaviour and strange crashes due to buffer overflows, memory leaks and the like).

(Also, you should wrap long source quotations like this in [source][/source] tags for posting on the boards. Shorter quotations can go in [code][/code] tags.

Share this post


Link to post
Share on other sites

This topic is 4198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this