Header Inclusion problems

Started by
1 comment, last by The Canary Man 22 years, 4 months ago
Can someone please check out this code and tell me why it won''t compile. Thanks. http://cronus.spaceports.com/~hose72/mgen.zip
Advertisement
You have a recursive dependency problem : CWrestler needs CTeam and vice-versa.

wrestler.h includes team.h which tries to include wrestler.h to get the definition of CWrestler, but the include guard prevents it. So team.h never gets the definition of CWrestler.

You should restructure your classes to use references, or pointers to the other class instead of an actual instance of the object. When you have an actual instance, the compiler needs to know the size of the data structure, and thus needs the complete definition. When you have a reference or a pointer, it justs takes into account the space for the 32-bit pointer.

Therefore, you can just tell it that there is a class named CTeam or CWrestler (by writing ''class CTeam;'' or ''class CWrestler;'') and not include the whole definition.

It is usually considered bad form to #include headers in headers, unless you REALLY cannot do without them. Just declare the needed symbols.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks for the help. That was exactly my problem.

This topic is closed to new replies.

Advertisement