Elevator Simulation

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



the main program (elevatorsimulation.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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..?
Last edited on
closed account (43RGz8AR)
"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?
Last edited on
ermmm...i thinks it hard for me to understand this line...

"I bet this is because you didn't include the said implemention file into the project. "


would you explain me in detail please..? what is the implementation file means..?
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?
Last edited on
closed account (43RGz8AR)
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...and this is the building.h code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Building class definition.
#ifndef BUILDING_H
#define BUILDING_H

#include "elevator.h"   // Elevator class definition
#include "floor.h"      // Floor class definition
#include "clock.h"      // Clock class definition
#include "scheduler.h"  // Scheduler class definition

class Building {

public:
   Building();                 // constructor
   ~Building();                // destructor
   void runSimulation( int );  // controls simulation 

private:
   Floor floor1;               // floor1 object
   Floor floor2;               // floor2 object
   Elevator elevator;          // elevator object
   Clock clock;                // clock object
   Scheduler scheduler;        // scheduler object

}; // end class Building

#endif // BUILDING_H 
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.
Last edited on
wait....i try to go through building.cpp file and i think maybe something wrong with the code...this is my building.cpp code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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 
closed account (43RGz8AR)
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.
Xenouis : do you mean that the class definition should not be in the header file..? i'm still confuse...

Moschops : would you show me what i should do so that the compiler know my
building.cpp
is part of my project please..?
Moschops : would you show me what i should do so that the compiler know my
building.cpp
is part of my project please..?


Not a clue. I don't use IDEs, and I've never even seen Quincy.
owh...okey...i try to solve it with Xenouis explanation...by the way, thanks a lot for giving your opinion...try to figure it out to...
closed account (43RGz8AR)
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...
closed account (43RGz8AR)
"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.
.i try to solve it with Xenouis explanation


It's the same explanation.

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".
YEAYYYYYYYYY!!!!!!!!!!!!

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
Topic archived. No new replies allowed.