|
-
November 28th, 2002, 06:52 PM
#1
Egg or chicken?
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
-
November 28th, 2002, 07:04 PM
#2
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
-
November 29th, 2002, 06:49 AM
#3
Yay!!
Thank you Jeff!! It works perfectly!!
You've saved me a bunch of money in aspirins :P
Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|