Recursive #including
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?
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
See:
http://www.gamedev.net/reference/articles/article1798.asp
Well you can use defines as in:
You can also read this nice article for more info.
Mikle
#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
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'
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 CmpDevQuote: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 __...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement