0

I'm having some trouble with cyclic dependencies. Imagine the following sample classes:

A class A with instance variables of types C and D and a method that uses these 2 types inside plus another type D:

#include "B.h"
#include "C.h"
#include "D.h"

class A {

private:
    B b;
    C c;

    void foo(D d);
};

A class B that depends on C:

#include "C.h"

class C;

class B {

private:
    C c;

    //some methods
};

A class C that uses methods of class D:

#include "D.h"

class D;

class C {

private:
    double x;

    void bar(D d);
};

A class D that has methods that use variables of the type B and C:

#include "B.h"
#include "C.h"

class B;
class C;

class D {

private:
    int i;

    void foo2(B b);
    void bar2(C c);
};

The error I'm getting is:

B.h:8:2 error: ‘C’ does not name a type

How can I solve this problem with cyclic dependencies?

Thanks

Edit: Updated class B:

#include "C.h"

class C;

class B {

private:
    C* c;

    B (B b) {*c = *(b.c);} //copy constructor
    //some methods
};
fc67
  • 409
  • 5
  • 17

0 Answers0