February 6th, 2004, 01:23 PM
This may sound a n00b question, but say I have two files, a.h and b.h. a.h contains a class, a, and b.h contains a class,b. class a has a member of type b, and vice versa for b. How would i arrange the include statements so that it compiles correctly?? thankyou.
February 6th, 2004, 01:34 PM
February 6th, 2004, 01:59 PM
You can't do this directly. One solution is to use a third header file called defs.h which has forward declarations for the classes. However that presupposes that you are using pointers to classes.
It *can* also be an indication that your design is bad. In any case, I would look at the design again and ask myself, why I would need to do this.
Get this small utility
to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
February 6th, 2004, 02:08 PM
Put a forward declaration in each file, eg
class b; // forward declaration
//note do NOT #include "b.h"
In the classes, stick to using pointers, otherwise you may find you run into problems, depending on what you're trying to do with the other class.
You may need to do the forward declaration the other way around, depending on which file your compiler looks at first.
This may be a sign of bad design, but is frequently inevitable. For example you have to do this to implement the 'visitor pattern' - a famous OO design solution.
Last edited by JonnoA; February 6th, 2004 at 02:11 PM.
February 6th, 2004, 03:53 PM
Take a look at the following FAQ...
Click Here to Expand Forum to Full Width