   Relations of classes in C++
  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 errorint 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
  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.   ;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.   ;  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 froma value type memberpassed or returned as value type (which in most cases is questionable in itself);  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.cppA 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?   ;  Put [font="'Courier New"]#include "a.h"[/font] and [font="'Courier New"]#include "b.h"[/font] into the CPP file.   ;  Thank you guys, now I can continue on developing my project! 