Jump to content
xisto Community
kvarnerexpress

Can Header Files Include Each Other?

Recommended Posts

Or will it create a circular include problem? What do you do if you need them to, and forward class declarations won't work?Forward class declarations only allow you to declare an instance of that class, but you can't access member functions, you have to include the header file. So if you use MS Visual C++ .NET and the code is placed in the header files, then what is the solution? I have two classes(forms) that need to include each other but it's not working. When I try it it stops recognizing the classes and gives errors like, "missing storage-class or type sepcifiers"I've also used about every combination of #ifndef, #define, #endif combo's I could think of. It's driving me crazy!Thanks for any help I might possibly get.

Share this post


Link to post
Share on other sites

No circular classes do not include the header files in each other's header. Instead, in the main or whatever part of the program uses the class that includes another class, you would include both files. Then in the two class files you just would have class Class1; Class2 {...}; and then in the second class you would do class Class2; Class1 {...}; Let me know if this makes sense becuase if not i will include an example that i have used in a class of mine last semester. Just PM or post a reply

Share this post


Link to post
Share on other sites

just my two cents worth, but maybe you should take the stuff you need for both classes out of their respective header files and put them in a single header file. sort of like a kind of factorisation.

 

for example:

 

//class1.hclass myFirstClass{    //functions, variables etc}//class2.hclass mySecondClass{    //more functions variables etc}

 

becomes

 

//myClasses.hclass myFirstClass {...};class mySecondClass {...};

 

then, if myFirstClass needs to reference mySecondClass, i think you can add this line above the myFirstClass definition:

class mySecondClass;

hope this works / helps. good luck.

 

oh and just out of curiousity, if you wouldn't mind posting that example fffantics, i had a bit of trouble understanding your last post but it seems like its good stuff. thanks! :rolleyes:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.