Skip to main content
GameDev.net gamedev.net
🔒 Locked

Relations of classes in C++

Started by DonceLT May 22, 2011 at 8:57 AM 5 replies 1.8k views
Original Post
DonceLT
DonceLT
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
Brother Bob
Brother Bob
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.
DonceLT
DonceLT

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.
Rattenhirn
Rattenhirn
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)
DonceLT
DonceLT
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?
Hodgman
Hodgman
Put [font="'Courier New"]#include "a.h"[/font] and [font="'Courier New"]#include "b.h"[/font] into the CPP file.
DonceLT
DonceLT
Thank you guys, now I can continue on developing my project!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.