2

I have 3 classes which I am going to refer to as A, B and C (names in this example are arbitrary). All 3 of these classes consist of a header-file and a cpp-file each.

I have the following dependencies:

  • A needs to know B as A has a member of type B
  • B needs to know A and C as B has members of both types
  • C needs to know A as C has a member of type A

I have tried out several options already (include this header here, forward-declaration of class there), but compilation was never succesful. I also couldn't extract any helpful information from answers to problems which solved circular inclusion problems between only 2 classes.

EDIT: Here a minimal (and hopefully sufficient) example of my current setup:
A.h

class A {
private:
    B myB;
};

B.h

class B {
private:
    A* myA;
    C myC;
};

C.h

class C {
private:
    A* myA;
}

I should perhaps also note that B::myA and C::myA are always going to point to the same A-instance.

Lux
  • 33
  • 4
  • `I have tried out several options already (include this header here, forward-declaration of class there), but compilation was never succesful.` Forward declarations are the way to go, we need an [MCVE]. – tkausl Dec 01 '19 at 17:55
  • 2
    Show us the (possibly abbreviated) declarations and definitions for your classes, and I'm 100% sure somebody here will give you a solution to your problem (or maybe more than one solution). – Adrian Mole Dec 01 '19 at 17:56
  • We really need to see a clear example of what you're trying to do, especially in regards to which members are pointers/references vs whole objects. At the moment I could read you explanation as wanting for each A to contain a B and then for each B to contain an A which isn't going to work since that leads to infinity. – TheUndeadFish Dec 01 '19 at 18:30
  • [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Dec 01 '19 at 18:37
  • @TheUndeadFish The requested example has been added. – Lux Dec 01 '19 at 18:40
  • The headers for class `B` and `C` don't need or want to include the header for `A`. The header for class `A` needs to include the header for class `B` – drescherjm Dec 01 '19 at 18:42
  • Per drescherjm: possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – Davis Herring Dec 01 '19 at 18:56

1 Answers1

1

As class C only includes a pointer to class A, it doesn't need the full definition for either A or B (which it doesn't use); so, just a statement declaring A as a class will suffice:

C.h

class A; // Declare that A is a class - then we can have a pointer to it...
class C {
private:
    A* myA;
}

Class B needs the definition of C, because it includes an instance of C; and, like class C, simply declaring A as a class (as is done already in C.h) will do:

B.h

#include "C.h" // Note: This header already declares "A" as a class!
class B {
private:
    A* myA;
    C myC;
};

Class A needs the definition of B, as it contains an instance of B. But note that B.h already includes C.h:

A.h

#include "B.h"
class A {
private:
    B myB;
};

Note that any other files that use one or more of A, B and C need only include the A.h header, as this will itself include the others.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Thank you very much, after applying your solution to my project and additionally including A.h in C.cpp it was compiled without issues. – Lux Dec 01 '19 at 19:04