Help with circular inclusion

Hi.

Whenever I try to make a lot of cpp and header files for a project, I always end up with problems with circular inclusion. I try to use forward declarations, but I still have the problems because I need to access that class's functions, create derived classes, etc. I've tried to find a fix for this, but I can't. Can someone please help me? The header guards don't work (#ifndefine, #define, #endif, and I've tried #pragma once). Nothing seems to work, and I keep getting stuck on this. How do I fix this?
Forward declaration is the general solution. Beyond that, it's hard to help without seeing the code in question. Would you mind posting it?

-Albatross
could I just #include in the cpp for using the classes functions and etc and the forward declaration in the header for the pointers and stuff, or would that not work? Also sometimes I get an error with my forward declarations about a redeclaration
yes overki11, that may work.
I'm not exactly trusting of you, as you have a bit of an infamous reputation, and your toxicity towards others.
That's generally a good strategy, to forward declare in the header and actually #include the relevant headers in the source file.

As for the error with redeclarations, it'd really help if we could see the source code in question, or at the very least be provided more information (such as what the error actually says).

Happy programming!
-Albatross
sure, albeit in a very excessive, toxic, and annoying way.

please stop replying to this thread, osucs1
Last edited on
Thank you Albatross, I can't provide the source code right now as all of the programs that I've run into this error have been scrapped due to my (childish) frustration. If this happens again I might dig this thread out of the grave and ask, otherwise, thank you for the help.
Alright, I just realized I cannot make derived classes without putting the #include in the header. Should I just make all my classes into 1 header?
That should generally be unnecessary, given that #include is pretty much a copy+paste.

We still don't really know much about what exactly you're trying to do. To make a shot in the complete dark, if your base class needs to maintain a reference to an instance of a derived class, you could forward declare the derived class in the header of the base class.

-Albatross
http://www.cplusplus.com/forum/articles/10627/ (see point 4: The "right way" to include)
It might have to do with the ordering in which you've put in your header guards. VS2010 is stupid like that. Personally I've never had to use #pragma once because I do:
1
2
3
4
5
6
7
#include<something>
#include<somethingelse>

#ifndef somename
#define samename

#endif 


Now I don't know if that's what you're doing or if that's working for you..but if you didn't do this, never hurts to try.
Topic archived. No new replies allowed.