Click to See Complete Forum and Search --> : Egg or chicken?


stimpy_z
November 28th, 2002, 05:52 PM
Hello y'all!

Here's a tricky situation. Probably it's quite silly, but I just don't see it (and it's giving me quite a headache)

let's say we have two classes: ClassA and ClassB. We need both classes to interact with eachother so in their private atributes we should find pointers to eachother. The headers should look somehting like...

classa.h

#ifndef __classa_h_
#define __classa_h_

#include "classb.h"

class ClassA {
public:
/* public members here*/
private:
ClassB * ptr_cb;
};

#endif

and...

classb.h

#ifndef __classb_h_
#define __classb_h_

#include "classa.h"

class ClassB {
public:
/* public members here*/
private:
ClassA * ptr_ca;
};

#endif


the trick here is that, no matter where the compiler starts, either one or another ".h" is not reached, so it tells me that either "ClassA" or "ClassB" is not defined.

I know what's going on, but I don't know how to solve it. I can't see a way to make both classes definition see eachother.

Any Ideas?

Steempee

jfaust
November 28th, 2002, 06:04 PM
Use forward references instead of including the files.

Use

class ClassA;

instead of

#include "classa.h"

Similarly for ClassB.

Then in the implementation file, include what you need. This is what you want to do in general, as it reduces include dependencies. Only include what is absolutely necessary in header files. This becomes more important the larger your project is. The last thing you want to do is start a 4 hour compile after changing a single header file.

Jeff

stimpy_z
November 29th, 2002, 05:49 AM
Thank you Jeff!! It works perfectly!!

You've saved me a bunch of money in aspirins :P

Thanks again!