C++ C++;-无法设置方法友谊
我试图在C++ C++;-无法设置方法友谊,c++,friend,circular-dependency,access-control,C++,Friend,Circular Dependency,Access Control,我试图在Playerclass上的classGameSimulator中将友谊设置为方法 由于某种原因,我犯了一个错误 GameSimulator.h: 您的GameSimulator类定义引用了指向玩家的指针,但不需要完整的类型。但是,您的玩家类定义确实需要完整类型的游戏模拟器 从GameSimulator.h中删除#include“Player.h”,并取消对Player.h中#include“GameSimulator.h”的注释。然后,向前声明类播放器在GameSimulator.h中
Player
class上的classGameSimulator
中将友谊设置为方法
由于某种原因,我犯了一个错误
GameSimulator.h:
您的GameSimulator类定义引用了指向
玩家的指针
,但不需要完整的类型。但是,您的玩家
类定义确实需要完整类型的游戏模拟器
从GameSimulator.h中删除#include“Player.h”
,并取消对Player.h中#include“GameSimulator.h”
的注释。然后,向前声明类播放器代码>在GameSimulator.h中
请注意,这些类的每个实现(.cpp文件)都需要包含另一个类的.h文件。GameSimulator
是一种不完整的类型,因此编译器在编译Player
时无法了解这些方法。相反,让GameSimulator
在GameSimulator.h
中向前声明Player
,并让Player.h
包含GameSimulator.h
。非常感谢!伟大的回答和伟大的解释!有效还有一个小问题:当我在gameSimulator.h中编写“class player”时,我让编译器知道了player类?@nimrod我不知道你的意思。@rebert English不是我最好的。我试试看。@nimrod如果你还有问题,请再说一遍。
#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_ */
#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_ */
../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