Recursive #including

Started by
9 comments, last by azherdev 17 years, 1 month ago
I have two classes, classA and classB. in two header files classA.h and classB.h including them within each others header files causes problems (from what i can see, since each of them has a #pragma once at the start, it won't get included the second time), how can i get around this without restructuring the code?
Advertisement
By restructuring the code. Don't worry, it's quick and painless. Read this.
You probably don't want to include them in each other's header files. Use forward references instead.

See:

http://www.gamedev.net/reference/articles/article1798.asp
too slow.... :)
Well you can use defines as in:

#ifndef __Some_Header_h__#define __Some_Header_h__Header codeHeader codeHeader code#endif //__Some_Header_h__


You can also read this nice article for more info.

Mikle
Mikle
Quote:Original post by Cowboy Coder
too slow.... :)


LOL, I second that...
Mikle
Thanks for the replies. I read that article and tried prototyping class B in classA.h, but i still have problems. I think it's cause a member function fo class B is being called...

classA.h

class B; //define class B here to allow it to be referenced in class A

class A{}; //prototype of class A

..

and then in classA.cpp

void classA::DoStuff(B* someB)
{
someB->DoThings();
}


gives me...

error C2027: use of undefined type 'classB'
...GameManager.h(10) : see declaration of 'classB'
You still need to #include b.h somewhere, otherwise you cannot use the details of the class. Just include it in the .cpp file, just after including a.h.
Quote:Original post by Mikle3
Well you can use defines as in:

*** Source Snippet Removed ***

You can also read this nice article for more info.

Mikle

Just to correct the quoted post, the standard says:
Quote:17.4.3.1.2 Global names [lib.global.names]
1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter
(2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global
namespace.
Quote:Original post by CmpDev
Quote:Original post by Mikle3
Well you can use defines as in:

*** Source Snippet Removed ***

You can also read this nice article for more info.

Mikle

Just to correct the quoted post, the standard says:
Quote:17.4.3.1.2 Global names [lib.global.names]
1 Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter
(2.11) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global
namespace.



Rules are made to be broken... I know that __ is for system stuff and _ is for private members, but hell, no need to be so tight about it...It's a define that's supposed to be unique. I think even MSVC provides you with a unique identifier starting in __...
Mikle

This topic is closed to new replies.

Advertisement