944,134 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 15753
  • C++ RSS
Mar 19th, 2005
0

C++ Beginner - #include recursion problem

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
j.kelly is offline Offline
10 posts
since Mar 2005
Mar 19th, 2005
0

Re: C++ Beginner - #include recursion problem

Can you give an example of how your headers look instead of just which includes the other?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 19th, 2005
0

Re: C++ Beginner - #include recursion problem

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
};
Reputation Points: 11
Solved Threads: 0
Newbie Poster
j.kelly is offline Offline
10 posts
since Mar 2005
Mar 19th, 2005
0

Re: C++ Beginner - #include recursion problem

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):
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 19th, 2005
0

Re: C++ Beginner - #include recursion problem

Thanks for your help. That was really giving me a headache

James
Reputation Points: 11
Solved Threads: 0
Newbie Poster
j.kelly is offline Offline
10 posts
since Mar 2005
May 18th, 2010
-3
Re: C++ Beginner - #include recursion problem
write a program which input a series of chracters until the user input .

by useing th recursive function??

can any one help
Reputation Points: 9
Solved Threads: 0
Newbie Poster
faaano is offline Offline
1 posts
since May 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Require assistance
Next Thread in C++ Forum Timeline: hi,





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC