-1

I try to set friendship to method from class GameSimulator on Player class.

for some reason I get error.

GameSimulator.h:

#ifndef GAMESIMULATOR_H_
#define GAMESIMULATOR_H_

#define NULL 0

#include "Player.h"

class GameSimulator {
public:
    void runSimulation();
    static GameSimulator* createGame();
    bool saveSession(string); // returns failure or successful
    bool loadSession(string); // returns failure or successful
    friend ostream& operator <<(ostream& out,GameSimulator& gs);
private:
    ~GameSimulator();
    GameSimulator();
    static GameSimulator* game;
    Player* *Population;
    unsigned numOfPlayers[4];
    int scores[4];
    unsigned numGeneration;
    unsigned numRounds;
};

#endif /* GAMESIMULATOR_H_ */

Player.h

#ifndef PLAYER_H_
#define PLAYER_H_
// includes

#include <iostream>
using namespace std;
enum PlayerType {row,col}; // player type
enum Strategy {TrustingFool,nasty,rnd,winStayLooseShift,titForTwoTats}; // strategy type
enum Move {Cooparate , Defect}; // move type

//#include "GameSimulator.h"

class GameSimulator;

class Player {
protected:
    int *myPayoffs;
    int *otherPayoffs;
    PlayerType playerType;// row or col player
    Strategy myStrategy; // what strategy to play
    unsigned roundID;   // #id iteration
public:
    friend bool GameSimulator::saveSession(string filename);
    friend bool GameSimulator::loadSession(string filename);
    virtual ~Player() = 0;
    virtual Move getMove() = 0;
    virtual string getStartegy() = 0;
    Player();
};


#endif /* PLAYER_H_ */

problems are:

../Player.h:30:56: error: invalid use of incomplete type ‘struct GameSimulator’
../Player.h:20:7: error: forward declaration of ‘struct GameSimulator’
../Player.h:31:56: error: invalid use of incomplete type ‘struct GameSimulator’
../Player.h:20:7: error: forward declaration of ‘struct GameSimulator’
../Player.h: In member function ‘bool GameSimulator::saveSession(std::string)’:
../Player.h:28:11: error: ‘unsigned int Player::roundID’ is protected
../GameSimulator.cpp:43:54: error: within this context
../Player.h:24:7: error: ‘int* Player::myPayoffs’ is protected
../GameSimulator.cpp:44:34: error: within this context
../Player.h:28:11: error: ‘unsigned int Player::roundID’ is protected
../GameSimulator.cpp:51:54: error: within this context
../Player.h:25:7: error: ‘int* Player::otherPayoffs’ is protected
../GameSimulator.cpp:52:34: error: within this context
../Player.h:28:11: error: ‘unsigned int Player::roundID’ is protected
../GameSimulator.cpp:58:33: error: within this context
../Player.h:26:13: error: ‘PlayerType Player::playerType’ is protected
../GameSimulator.cpp:71:34: error: within this context
make: *** [GameSimulator.o] Error 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
nimrod
  • 132
  • 1
  • 1
  • 8
  • 1
    `GameSimulator` is an incomplete type, so no way for compiler to know about these methods when compiling `Player`. Instead, let `GameSimulator` forward declare `Player` in `GameSimulator.h`, and let `Player.h` `#include` `GameSimulator.h`. – jxh Jun 17 '12 at 00:54

1 Answers1

1

Your GameSimulator class definition refers to a pointer to Player, but doesn't need the complete type. However, your Player class definition does need the complete type of GameSimulator.

Remove the #include "Player.h" from GameSimulator.h and uncomment the #include "GameSimulator.h" in Player.h. Then, forward declare class Player; in GameSimulator.h.

Note that the implementations of each of these classes (the .cpp files) will each need to include the .h file for the other.

robert
  • 33,242
  • 8
  • 53
  • 74