circular dependencies, forward doesn't help
hi,
a little abstraction to demonstrate my problem:
Code:
------------
class1.h
-----------
#ifndef CLASS1_H_
#define CLASS1_H_
#include "class2.h" // inclusion
class class2; // and forward
class class1
{
private:
class2 aRererence;
// --- gcc says: "error: field 'aRererence' has incomplete type" ---
public:
class1() :aReference( class2& aR ) {aReference = aR;};
// --- error according to the missing type of aReference ---
};
#endif /*CLASS1_H_*/
------------
class2.h
------------
#ifndef CLASS2_H_
#define CLASS2_H_
#include <vector>
#include "class1.h" // inclusion
class class1; // and forward
class class2
{
private:
std::vector<class1> someContainer;
public:
class2();
};
#endif /*CLASS2_H_*/
-----------------------------------------
What is the correct way around this error? I guess I could declare the
field as pointer, but I wanted to use the c++ reference mechanism so I can't let out the #includes, because the compiler have to know the needed memory for the initialisation.
Yes I read the rules about using include or forward and tried the most combinations without luck.
And for the people asking what I exactly want to do:
class2 is a simply state of a finite state machine holding a set of transitions/conditions (=class1). Why does the conditions have to know about the state they belongs to and therefore need to include that class? because to a transition belongs a reference to the state it should switch to if it becomes true. so theres a circular dependency and i've no clue about how to redesign that without losing functionality.
Thanks for your help in advance.