tags:

views:

62

answers:

2

Hi i have a problem, I try to do the follow, class A depends on class B and vice versa like this

class A;

 class B{
   A foo;
   friend B A::doSomething();

 };

class A {
   B bar;
   B doSomething;
};

The friend declartion does not work because A is an incomplete declartion. Is there any way to avoid this? (Swapp classes declaration order is not a solution I did not want to construct a more complicated example where swapping does not work anymore)

+5  A: 

What you're trying to do right now would make your classes infinitely large (as I understand, bar and foo are class member variables, hence this means that B includes A, which includes B, ...).

You could use pointers and store A* foo or B* bar in one of your classes or somehow redesign your application to avoid this circular dependency.

Kotti
A: 

Nest the two classes. This gives far more favourable rules about where and when you can declare and define what.

In addition, I'd suspect that your design may just be borked. You might want to post the actual innards of A and B so that we can give more advice.

DeadMG