Skip to main content
GameDev.net gamedev.net
🔒 Locked

Cross including headers

Started by Mr Cucumber Sep 11, 2000 at 2:00 PM 2 replies 150+ views
Original Post
Mr Cucumber
Mr Cucumber
In my program I need to cross include two headers. With that I mean that header1 is dependant on header2 and header2 is dependant on header1. This causes problem because of the: #ifndef header1 #define header1 //code... #endif This gives me errors of such kind that things hasn''t been declared or something. What could be done?
Kensai
Kensai
I doubt you really need to cross-include headers. I would just put forward declarations at the top of each for the stuff in the other header that you are referencing.
Prosper/LOADED
Prosper/LOADED
If you only need a pointer in one of the classes, you don''t need the whole declaration. Just put something like that :

class a;

class b
{
a * ptr;
}

felisandria
felisandria
Thou Shalt Not Commit Circular Includes!!

First thing to do is make sure you really DO need Class A to know Class B and Class B to know Class A. What kind of relationship are you attempting there? Generally when I see circular dependencies it''s because something was done incorrectly.

If you really, really do have to do that, do what Kensai said and use a forward class reference. To do this, go to Class A''s header file and put:

class Class B;

before your declaration of Class A. Do the same with Class A in Class B''s header.
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.