-2

Suppose i have a header which contain these two classes

class A:public class B{
 // code
};
class B 
{
protected:
A a_object;
};

when the compiler compiles this include file,when it comes to Class A ,it sees class A inherits from B but it doesn't reach to Class B definition.So it gives an error. and if i reverse the order of both classes,it gives error due to a_object as it doesn't see Class A definition.

So how to solve this problem? and assume that i restricted to this include file to have class A and B definitions.

Thanks

Yahia Farghaly
  • 857
  • 2
  • 10
  • 20

2 Answers2

1

Here is a simplified version of your problem:

class X {
    X x;
};

A class cannot embed an object of its own type.

Specifically, your class B embeds an object of type A, which by inheritance is also of type B.

rom1v
  • 2,752
  • 3
  • 21
  • 47
1

If you really have to have a hierarchy like that you can do something like the following.

class A;

class B 
{
protected:
  A* a_object;
};

class A: public B {
  // code
};
london-deveoper
  • 533
  • 3
  • 8