当我尝试通过类似的指针创建属性到另一个类时,为什么我收到错误:
#ifndef SQUARE_H
#define SQUARE_H
#include <string>
//using namespace std;
#include "Player.h"
class Square
{
public:
Square(int);
void process();
protected:
int ID;
Player* PlayerOn; <---
};
#endif
并且Player类是:
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
//using namespace std;
#include "Square.h"
class Player
{
public:
Player(int,int);
// ~Player(void);
int playDice();
private:
int ID;
int money;
};
#endif
我收到:
syntax error missing ; before * (on the declaration of Player* PlayerOn;)
和缺少的类型说明符(在同一行......)
答案 0 :(得分:3)
看起来像递归包含的问题。你应该在方形类中使用前向声明。
#ifndef SQUARE_H
#define SQUARE_H
#include <string>
//using namespace std;
class Player; //You will have to use the #include "player.h" in your .cpp
class Square
{
public:
Square(int);
void process();
protected:
int ID;
Player* PlayerOn; <---
};
答案 1 :(得分:2)
问题是你在Player.h中包含Square.h,所以当你得到Player* PlayerOn;
未定义播放器时
您的Player.h中没有#include "Square.h"
可以使用此代码。如果实际代码更复杂,请使用前向声明Square #include "Square.h"
class Square;