0

So, I'm having a problem where:

class A needs to know about class B, class B needs to know about C, and class C needs to know about A. It's essentially a circle, so I get definition errors. I tried forward declaration, but whatever's on top, doesn't know about what else goes on at the bottom.

How would I go about a situation like this?

Thanks

David

dajee
  • 934
  • 2
  • 13
  • 30
  • 5
    You start by breaking the circular dependency. For a similar question, see [this SO answer](http://stackoverflow.com/q/4110479/960195). – Adam Mihalcin Apr 10 '12 at 03:12
  • What do you mean by "whatever's on top, doesn't know about what elese goes on at the bottom?" – cloudygoose Apr 10 '12 at 03:17
  • in a header file say you declare: class A, B C in that order. A still needs to know about either B or C, but since it's on top, can't use them. If that makes sense. – dajee Apr 10 '12 at 03:18
  • Cant you just declare the class ..? class A; class B;class C; – Anerudhan Gopal Apr 10 '12 at 13:52

5 Answers5

2

Assuming this is something simple, the comment above by Adam Mihalcin is correct in that the similar question answers it. But I'll code it out anyways.

Assuming you have this (method definitions don't matter) :

class A
{
    B* ptrB;
}

class B
{
    C* ptrC;
}

class C
{
    A* ptrA;
}

Then you can, as Adam linked to, just forward-delcare all 3 of them like this:

class A;
class B;
class C;

And put that block above all 3 of them. The important thing here though is that this is when there are pointers to the other classes, not composition where they're a part of the class. You can use forward-declaration to allow things to compile correctly when dealing with pointers, but not with other data types. So if there was this:

class D
{
    B myB;
}

With everything else the same as above, you'd need to have the "real" definition for B above the "real" definition for D. But the rest could be the same. Forward declaration only "works" for pointers.

You MUST break your dependency loop somewhere with a pointer though. If there's never a pointer, then the data structure is "infinite" and thus doesn't work. That's because in my example, a D always contains a B. But what if an A always contained a B, and a B always contained a C, and a C always contained an A? Well that final A needs another B, which needs another C, which... I hope you get the idea. But with pointers, you can loop it. With composition (not pointers) it can't loop around.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
0

If the classes are tightly coupled, then you can do with

  • Declare the classes with no method implementations, possibly using forward declarations, then implement the methods (possibly in an implementation file).

Otherwise, you can

  • Factor out the more abstract interface(s) sufficient for the classes to work without knowing full details about each other.

But most probably you have a design level error, and if so, just fix that.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

What you can do is forward declare class A in in the header file for class C, then you in the .cpp file, you include the header for class A.

For a reference on how to forward declare class A, take a look at this reference - Forward declarations in C++

josephthomas
  • 3,256
  • 15
  • 20
0

most probably, u've just got an design level error.

However, if u have to do this, for example , just try to utilize forward-declare by declaring an empty class A,B without presenting any detail implementation in C's header file. then include A and B's header in C's implementation file when u need to use them.

Remember, u can only use pointer in the declaration of class C.

House.Lee
  • 241
  • 1
  • 3
  • 12
0

In your header files use next construction:

#ifndef MYCLASS_HPP__20120410__0747
#define MYCLASS_HPP__20120410__0747
// Your definions of class, struct, etc...
#endif//

And all included file (except self Myclass.hpp) should be in class-header file Myclass.hpp. He must be included in Myclass.cpp file.