1
\$\begingroup\$

I've read this question, and I think it has some good answers and discussion, but it appears to focus primarily on rendering.

I have a different problem in the management of a game entity's state. What I end up with is this:

Game -> Player -> FSM -> State -> Player

Upon entry into a state a pointer to Player must be passed through, but no matter what I do in C++ it has trouble resolving this relationship hierarchy.

A good deal of how I'm designing my FSM is like this article. However, that is a psuedo implementation, and does not use headers. Without moving completely over to behavior trees is there another design pattern that will eliminate this circular dependency?

\$\endgroup\$
  • \$\begingroup\$ I don't follow what the problem is. There is no reason conceptually that what you are saying you're doing wouldn't work. Post a (simplified) snippet of problematic code and the related errors. \$\endgroup\$ – Sean Middleditch Jul 30 '14 at 15:41
  • \$\begingroup\$ I reverted some of my changes so the error no longer appears, but I can re-implement and compile again so that I can post the code here. I've been reading up on forward declarations, but they seem to get mixed reactions, and some developers argue if I have a circular dependency at all then it must have been designed wrong. \$\endgroup\$ – user6214 Jul 30 '14 at 15:53
  • 2
    \$\begingroup\$ "some developers" aren't writing your game. Don't get caught up in academic arguments of design purity from people who aren't invested in producing the best version of your end product. There's generalized good design advice which you should follow when you can but don't get hung up on it. :) \$\endgroup\$ – Sean Middleditch Jul 30 '14 at 17:14
  • \$\begingroup\$ Thank you for that encouragement. I've been in the Java world for a long time, and so getting deeper into C++ is a mix of excitement and trepidation. \$\endgroup\$ – user6214 Jul 30 '14 at 17:37
4
\$\begingroup\$

you can use a forward declaration of the class that are in circular dependency.

Here an explanation

class firstClass; //Here the forward declaration
class secondClass
{
  public:
  private:
};

Then in the cpp of the second class you can include the firstClass header file.

\$\endgroup\$
  • 1
    \$\begingroup\$ Using forward declaration whenever possible in header files is good practice, as it will minimize your compile times. The main restriction is you won't have access to any functionality from the class, so you'll have to use pointers and you won't be able to call any functions (in the header file). \$\endgroup\$ – Sergio Jul 30 '14 at 16:34
  • \$\begingroup\$ So it turns out after a lot of research and a very brief sandbox attempt I reproduced the error. It is ultimately a problem with forward declaration vs. using includes everywhere. \$\endgroup\$ – user6214 Jul 30 '14 at 17:39