I have gotten many cyclic dependencies recently when my header files includes each other. See also here: What are forward declarations in C++?
I actually do not get totally why its a cycle. When the compiler looks inside the header-file of the include, why does it not recognize the class declaration?
Is there an more elegant/other way to break these cycles instead of a forward declaration of the other class? I do not like that there is a other class declaration in front of my current class. E.g.
#include "Wheel.h" // Include Wheel's definition so it can be used in Car.
#include <vector>
class Wheel;
class Car
{
std::vector<Wheel> wheels;
};
I hope I got your stuck point.
To explain this, it is better to examine from the view of compiler.
Compiler does for
Car.h:#include "Wheel.h"with its content.#include <vector>with its content.At this step, translation unit for
Car.hseems like this.At this point, at
Car* car;line,class Cardeclaration (declaration is enough for pointer types) is needed to defineclass Wheelthat's why you need forward declaration because you should tell to compiler that there is aclass Carand it will be defined soon.If you comment out
class Carat line 1 compiler could not know whether there will be aclass Caror not.As far as I know there are extra restrictions related with one definition rule, maybe someone else might explain that.
It is not possible to have 'more elegant' way for now.