| | |
C++ Beginner - #include recursion problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 10
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Mar 2005
Posts: 10
Reputation:
Solved Threads: 0
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
};
#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
};
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):
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:
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.
C++ Syntax (Toggle Plain Text)
#ifndef FLOOR_H #define FLOOR_H #include <memory> class Control; class Floor { public: Floor(void); ~Floor(void); private: std::auto_ptr<Control> control;//to send requests }; #endif
C++ Syntax (Toggle Plain Text)
#ifndef CONTROL_H #define CONTROL_H #include <memory> class Floor; class Lift; class Control { public: Control(); ~Control(void); private: std::auto_ptr<Floor> floor;//to update floor display std::auto_ptr<Lift> lift;//to tell lift what to do }; #endif
C++ Syntax (Toggle Plain Text)
#ifndef LIFT_H #define LIFT_H #include <memory> class Control; class Lift { public: Lift(); ~Lift(void); private: std::auto_ptr<Control> control;//to send requests }; #endif
C++ Syntax (Toggle Plain Text)
class Floor { public: Floor(); ~Floor(); }; class Lift { public: Lift(); ~Lift(); private: Floor floor[20]; // 20 story building }; class Control { public: Control(); ~Control(); private: Lift lift; };
I'm here to prove you wrong.
![]() |
Similar Threads
- Recursion problem (Java)
- Recursion help....PLEASE!!! (C++)
- Recursion Problem (Java)
- recursion problem (C)
- gcd problem (C++)
Other Threads in the C++ Forum
- Previous Thread: creating a window based dialog
- Next Thread: C++ Beginner - Enumerations
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






