Sign in to follow this  
DonceLT

Relations of classes in C++

Recommended Posts

DonceLT    121
I am making a project with many classes, which have "relations" with each other. I make that by sample below(with guariands). That sample below is working, but if I include a.h instead b.h in main.cpp, then I get "error C2079: 'B::a' uses undefined class 'A'". I guess that happens because main.cpp calls A.h, when A.h calls B.h, and B.h doesn't include A.h anymore, because it's guarded(A_INC is defined), and class B don't know what class A is. Is this correct, and if yes, how to solve this? I would like to have ability to include those two files independently(not #include "b.h" and then #include "a.h", I know this works).

Sample code:

"main.cpp"
[code]
#include "b.h"
//#include "a.h" //this without upper include causes error

int main()
{
return 0;
}
[/code]

"a.h"
[code]
#ifndef A_INC
#define A_INC

#include "b.h"
class B;

class A {
B *b;
};

#endif
[/code]

"b.h"
[code]

#ifndef B_INC
#define B_INC

#include "a.h"
class A;

class B {
A a;
};
#endif
[/code]

Share this post


Link to post
Share on other sites
Brother Bob    10344
I believe the solution is to not include "b.h" from "a.h". You're storing a pointer to B in A so the forward declaration of B is enough, and the cyclic inclusion problem is because you're including the other header in each of the two files. So just break the cyclic dependency by not including the headers that is not needed.

Unless it's requires to store by value, consider storing the A in B by pointer also. That way, you don't need to include "a.h" in "b.h" either.

Share this post


Link to post
Share on other sites
DonceLT    121
[quote name='Brother Bob' timestamp='1306055705' post='4814165']
I believe the solution is to not include "b.h" from "a.h". You're storing a pointer to B in A so the forward declaration of B is enough, and the cyclic inclusion problem is because you're including the other header in each of the two files. So just break the cyclic dependency by not including the headers that is not needed.

Unless it's requires to store by value, consider storing the A in B by pointer also. That way, you don't need to include "a.h" in "b.h" either.
[/quote]

Wow, this simple solution works, thank you very much! Now I'll try to apply this to my project.

Share this post


Link to post
Share on other sites
Rattenhirn    3114
Brother Bob is spot on.

As a general rule, never put an include statement into a header, unless absolutely necessary.

It's only necessary when the header file to include declares a type that's:
[list]
[*]inheriting from
[*]a value type member
[*]passed or returned as value type (which in most cases is questionable in itself)
[/list]

Share this post


Link to post
Share on other sites
DonceLT    121
It works for this example, but it's just a simple pointer then, I can't use it as that class, it's methods and variables. For example if I add this in main.cpp
A a;
a.b->nr;
where nr is public variable of class B. In this example I get "error C2027: use of undefined type 'B'".

Any more suggestions please?

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