hai...i'm running windows 7 x64 and using Quincy 2005 as my compiler...i try to run/execute the Elevator Simulation program that i get from deitel&deitel book...all .cpp file is okey and successful when i compile...but the problem is there three error when i try to run/execute the program...this is the error that i got :
elevatorsimulation.o: In function `main':
ElevatorSimulation.cpp:18: undefined reference to `Building::Building()'
ElevatorSimulation.cpp:23: undefined reference to `Building::runSimulation(int)'
ElevatorSimulation.cpp:27: undefined reference to `Building::~Building()'
collect2: ld returned 1 exit status
Unsuccessful build
// Driver for the simulation.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "building.h" // Building class definition
int main()
{
int duration; // length of simulation in seconds
cout << "Enter run time: ";
cin >> duration;
cin.ignore(); // ignore return char
Building building; // create the building
cout << endl << "*** ELEVATOR SIMULATION BEGINS ***"
<< endl << endl;
building.runSimulation( duration ); // start simulation
cout << "*** ELEVATOR SIMULATION ENDS ***" << endl;
return 0;
}
// end main
I read from other forum that they said i must include all translation units...and this is my second problem...i don't know how to include all translation units...
can someone teach me how to solve this error please..?
"undefined reference to" This is because your linker (ld) is telling you "Hey I can't find these function implementions in any code files you gave me". I bet this is because you didn't include the said implemention file into the project.
EDIT: Sorry I should clarify that a little. Implemention files are "*.cpp" files. When you made the project did you add the accompanying building.cpp file to the project in your IDE?
Somewhere you wrote a cpp file containing the function
Building::runSimulation(int)
The linker, which joins together all your code after it has been compiled, can't find it. Where is it? Is it being compiled? If you're using an IDE, is it part of your "project" or whatever the IDE calls it?
Implementation is the actually code and/or function that does the work.
Definitions/Declarations are the same name function but without the actual code that does anything.
1 2 3
/// Declaration
/// this part is in my imaginary "def.h" file
void myFunction(void* params);
1 2 3 4 5 6 7 8 9
#include <iostream>
#include "def.h"
/// Implementation
/// this part is in my imaginary "impl.cpp" file
void myFunction(void* params)
{
// do something here
std::cout << "Hello, World!" << endl;
}
i try to find it and according to the ElevatorSimulation.cpp, it should be in building.h file
ElevatorSimulation.cpp does not specify where function implementation is written. It is commonly not the role of a *.h file to contain the implementation.
Commonly, as in this case, the header file contains the prototype of the functions, not the implementation; a prototype is only the name of the function, the input types and the return type. This is all the compiler needs to compile the code. The implementation is commonly put in a separate cpp file.
// Member-function definitions for class Building.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "building.h" // Building class definition
// constructor
Building::Building()
: floor1( Floor::FLOOR1, elevator ),
floor2( Floor::FLOOR2, elevator ),
elevator( floor1, floor2 ),
scheduler( floor1, floor2 )
{
cout << "building created" << endl;
} // end Building constructor
// destructor
Building::~Building()
{
cout << "building destroyed" << endl;
} // end ~Building destructor
// function to control simulation
void Building::runSimulation( int totalTime )
{
int currentTime = 0;
while ( currentTime < totalTime ) {
clock.tick(); // increment time
currentTime = clock.getTime(); // get new time
cout << "TIME: " << currentTime << endl;
// process person arrivals for currentTime
scheduler.processTime( currentTime );
// process elevator events for currentTime
elevator.processTime( currentTime );
// wait for Enter key press, so user can view output
cin.get();
} // end while
} // end function runSimulation
No see your not understanding that a header file should never contain the actual implementation. Using my example above I would include "def.h" in my main file and also include the "impl.cpp" as part of the project so that your compiler and linker knows to compile and link the function into your resulting executable.
My guess is that you are not compiling building.cpp, or your linker does not know it is part of your project. This is a problem with your IDE settings.
What have I been trying to tell you? That definitions go in headers and implementations go in ".cpp" files. I was replying to what you said before you edited your post. The result confusion. This is why I put "EDIT:" before I change my post and keep the previous information up so that other forum readers will understand what happened. Also the reason mine and Moschops reply might be unhelpful is you really need to give more information, like what IDE are you using?
i'm using Quincy 2005 (is this IDE you mean for ?)...also try to use Visual C++ 2010...but it didn't show me where the error is...or i don't know how to use it...
sorry for the confusing reply...my line slow maybe...i try to understand what implementation that should go in "**.cpp" file and what definition should go in header file...sorry for my dumbness...
"sorry for my dumbness" don't be ashamed in any way I only want to help, but I am afraid this is also out of my understanding and like above I've never heard of Quincy 2005. I'm sorry if I seem to have put you down.
Dig out the instructions for your IDE, which is Quincy 2005, and read how it works. I suspect it uses "projects" or some such word. You need to "add" building.cpp to your "project".
thanks for all your explanation...i can run the program for now...and my next step is to edit a little coding so that i can choose how many elevator that i want to run...
THANK YOU VERY VERY VERY MUCH XENOUIS AND MOSCHOPS!!!!!!!!!
**the problem is that i didn't put it in "project"...so that the coding is independently run and not link with other code...i think that in my logic way...hehehe...
**thank you so much you guys...you two are very very very helpful...thank you so much once again... :D