Sign in to follow this  
Winegums

Recursive #including

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Well you can use defines as in:


#ifndef __Some_Header_h__
#define __Some_Header_h__

Header code
Header code
Header code

#endif //__Some_Header_h__




You can also read this nice article for more info.

Mikle

Share this post


Link to post
Share on other sites
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'

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 __...

Share this post


Link to post
Share on other sites
I generally just use the filename and extension in all caps:


if the file is class_b.h



#ifndef CLASS_B_H
#define CLASS_B_H

// some code

#endif



if the file is ClassB.h



#ifndef CLASSB_H
#define CLASSB_H

// some code

#endif


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

Sign in to follow this