C++ Beginner - #include recursion problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 10
Reputation: j.kelly is an unknown quantity at this point 
Solved Threads: 0
j.kelly j.kelly is offline Offline
Newbie Poster

C++ Beginner - #include recursion problem

 
0
  #1
Mar 19th, 2005
I am new to C++ and cant get my program to compile because of #include recursion.

I have 3 classes A, B, C.

A includes B.h and C.h
B includes A.h
C includes A.h and B.h

They include each other because they either need to send messages between themselves.

No inheritance is involved.

I've tried using:
#ifndef
#define
.........
#endif
but because A and B are included twice they dont get included the second time.

Any ideas for a C++ beginner who's spent the last 2 days trying to botch something togeather?

Thanks,
James
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Beginner - #include recursion problem

 
0
  #2
Mar 19th, 2005
Can you give an example of how your headers look instead of just which includes the other?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 10
Reputation: j.kelly is an unknown quantity at this point 
Solved Threads: 0
j.kelly j.kelly is offline Offline
Newbie Poster

Re: C++ Beginner - #include recursion problem

 
0
  #3
Mar 19th, 2005
As they stand they just recur:

#include "Lift.h"
#include "Control.h"
class Floor
{
public:
Floor(void);
~Floor(void);
private:
Control control;//to send requests
};

#include "Floor.h"
#include "Lift.h"
class Control
{
public:
Control();
~Control(void);
private:
Floor floor;//to update floor display
Lift lift;//to tell lift what to do
};

#include "Control.h"
class Lift
{
public:
Lift();
~Lift(void);
private:
Control control;//to send requests
};
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ Beginner - #include recursion problem

 
0
  #4
Mar 19th, 2005
If you want your classes to use each other, you pretty much need to do something like this using forward declarations and pointers (preferably smart pointers as they make your life easier):
  1. #ifndef FLOOR_H
  2. #define FLOOR_H
  3.  
  4. #include <memory>
  5.  
  6. class Control;
  7.  
  8. class Floor
  9. {
  10. public:
  11. Floor(void);
  12. ~Floor(void);
  13. private:
  14. std::auto_ptr<Control> control;//to send requests
  15. };
  16.  
  17. #endif
  1. #ifndef CONTROL_H
  2. #define CONTROL_H
  3.  
  4. #include <memory>
  5.  
  6. class Floor;
  7. class Lift;
  8.  
  9. class Control
  10. {
  11. public:
  12. Control();
  13. ~Control(void);
  14. private:
  15. std::auto_ptr<Floor> floor;//to update floor display
  16. std::auto_ptr<Lift> lift;//to tell lift what to do
  17. };
  18.  
  19. #endif
  1. #ifndef LIFT_H
  2. #define LIFT_H
  3.  
  4. #include <memory>
  5.  
  6. class Control;
  7.  
  8. class Lift
  9. {
  10. public:
  11. Lift();
  12. ~Lift(void);
  13. private:
  14. std::auto_ptr<Control> control;//to send requests
  15. };
  16.  
  17. #endif
If you don't want to do that, you would be better off restructuring the classes so that they don't depend on each other:
  1. class Floor
  2. {
  3. public:
  4. Floor();
  5. ~Floor();
  6. };
  7.  
  8. class Lift
  9. {
  10. public:
  11. Lift();
  12. ~Lift();
  13. private:
  14. Floor floor[20]; // 20 story building
  15. };
  16.  
  17. class Control
  18. {
  19. public:
  20. Control();
  21. ~Control();
  22. private:
  23. Lift lift;
  24. };
Control manipulates Lift through the Lift public interface and Lift manipulates the array of Floor through the Floor public interface. Any messaging is done that way. Of course, I don't know how your design is set up, so this suggestion may not work without significant changes.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 10
Reputation: j.kelly is an unknown quantity at this point 
Solved Threads: 0
j.kelly j.kelly is offline Offline
Newbie Poster

Re: C++ Beginner - #include recursion problem

 
0
  #5
Mar 19th, 2005
Thanks for your help. That was really giving me a headache

James
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC