# Relations of classes in C++

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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"
 #include "b.h" //#include "a.h" //this without upper include causes error int main() { return 0; } 

"a.h"
 #ifndef A_INC #define A_INC #include "b.h" class B; class A { B *b; }; #endif 

"b.h"
 #ifndef B_INC #define B_INC #include "a.h" class A; class B { A a; }; #endif 

##### Share on other sites
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 on other sites

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.

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

##### Share on other sites
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:

• inheriting from
• a value type member
• passed or returned as value type (which in most cases is questionable in itself)

##### Share on other sites
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'".

##### Share on other sites
Put [font="'Courier New"]#include "a.h"[/font] and [font="'Courier New"]#include "b.h"[/font] into the CPP file.

##### Share on other sites
Thank you guys, now I can continue on developing my project!

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631429
• Total Posts
3000034
×