1

For example a little Computergame with three Classes Player, Bot and Game

Player has a Method that checks if the Player collide with a bot

// Player.h
#include Game.h   
#include Bot.h

class Player {
    private:
    bool collision(Game g)  {

       for (Bot bot: g.bots)
       ...
     }

};

The Bot.h (kept simple, of cause it has some other attributes like actual position and so far)

// Bot.h


class Bot {
    public:
    Bot()
};

The Gameclass handles the Gameloop and the List of Bots

//Game.h
#include Bot.h    
#include Player.h

class Game {

public:
Player player:
std::vector<Bot> bots
void loop() { player.collision() }

};

So here we have the problem that Game.h includes Player.h and the other way around.

How can I resolve this?

  • 3
    Why does player.h include game.h? In the code you have posted there is no reason. If there is a good reason for player.h to include game.h then we need to know what it is before we can give a solution. – john Sep 21 '13 at 13:26
  • john, you're right, thanks. I've corrected it. The for-loop should be in player-class and not in game. – user2796729 Sep 21 '13 at 13:33

4 Answers4

3

While the other answers here are certainly technically correct, whenever I come across a situation like this I see it as pointing out a potential flaw in my design. What you have is cyclic dependencies, like so:

enter image description here

This isn't just a problem for your implementation, it means that there is too much coupling between classes, or, conversely, too little information hiding. This means that you can't design the Player independently of the Game, for example.

So if possible, I'd prefer a situation like this, where the Game, as the controller delegates work out to other classes.

enter image description here

One way to do this is for the Game to pass references to its own properties as and when the Player needs them.

For example have collision take a bots parameter rather than the `Game

bool collision(const std::vector<Bot&> bots)  {
    // note pass objects by const ref is usu preferred in C++ to pass by value

   for (Bot bot: g.bots)
   ...
 }

(Note in a more sophisticated approach, it could pass interfaces, i.e. abstract base classes, onto itself).

If all else fails, you can go back to using a forward declaration.

TooTone
  • 7,129
  • 5
  • 34
  • 60
2

In this case the easiest thing would be a forward declaration, and moving some code from the header file to the source file.

Like this

Player.h

#include "Bot.h"

class Game; // forward declaration

class Player {
    private:
    bool collision(Game g);
};

Player.cpp

#include "Player.h"
#include "Game.h"

bool Player::collision(Game g)  {

   for (Bot bot: g.bots)
   ...
}

The forward declaration tells the compiler that Game is the name of a class but nothing else. So the Player::collision method must be moved to the Player.cpp file where the full definition of Game is available.

john
  • 85,011
  • 4
  • 57
  • 81
1

I dont think you can do this since If A contains B, and B contains A, it would be infinite size. Infact you can create two classes that store pointers to one another, by using the forward declaration, so that the two classes know of each other's existence

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Forward declaration is required - http://en.wikipedia.org/wiki/Forward_declaration

Ed Heal
  • 59,252
  • 17
  • 87
  • 127