0

I have a file which is strictly for defines, global data, typedefs etc...

gamedata.h

#include <utility>
#include "player.h"

namespace GameData {
    enum Color {
        BLACK = 0,
        WHITE = 1
    };
    typedef typename std::pair<char, int> Position;
    typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move;
    typedef typename std::pair<Player*, Player*> Players;
};

I am having two problems:

(1) Player class is not found - even though I've included it with the header

#include <string>
#include "board.h"
#include "gamedata.h"

class Player {
public:
    virtual ~Player() {}
    virtual Move getMove(Board &c) = 0;
};

(2) The Move getMove function in the player header cannot resolve Move

`Move` does not name a type 

My best guess is that this is happening because it is a circular include where both need to include each other.

So I forward declare Player in gamedata.h

class Player;
namespace GameData {
    enum Color {
        BLACK = 0,
        WHITE = 1
    };
    typedef typename std::pair<char, int> Position;
    typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move;
    typedef typename std::pair<Player*, Player*> Players;
};

Which fixes the Player problem, but now the move is still wonky.

// None work
GameData::Move
Move

Code:

http://pastebin.com/i0GJz6xt

Edit 1

I would like to change this:

    typedef typename std::pair< std::pair<char, int>, std::pair<char, int> > Move;

to

    typedef typename std::pair<Position, Position> Move;

But I thought it was producing the error so I didn't revert it back yet.

user2997491
  • 135
  • 7

1 Answers1

1

Alright I found the answer after 50 searches haha. When I said I tried to forward declare class Player I didn't remove the #include "player.h" which was the issue.

A quote from Peter 87:

You have a circular dependency. janitor.h includes lunch.h. lunch.h includes janitor.h, but the header guard prevents janitor.h from being included and lunch.h fails because it depends on what is in janitor.h.

To prevent this problem you can use forward declaration.

Instead of #include "janitor.h" you write class janitor; This will work as long as all you have is pointers or reference to janitor objects. If you use the object you will need to include the header so in the source files you will probably have to include them. Do the same with all your other headers, not just janitor.h. A good rule is to use forward declarations if it's enough and only include if you have to.

Source: http://www.cplusplus.com/forum/general/56475/

user2997491
  • 135
  • 7
  • 1
    I thought you said in your original question that you tried this and it still didn't work? Could you please explain Peter87's comment so I can understand the difference between what he's suggesting and what you initially tried? Thanks :) – starsplusplus Jan 16 '14 at 15:02