作为属性指向另一个类的指针
Pointer to another class as a property
为什么当我尝试通过指针创建另一个类的属性时收到错误:
#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;)
和缺少类型说明符(在同一行…)
看起来像是递归包含的问题。你应该在你的square类中使用前向声明。
#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; <---
};
问题是你在Player.h中包含了Square.h,所以当你得到Player* PlayerOn;
Player未定义
或者在Player.h中没有#include "Square.h"
,这将与此代码一起工作。如果实际代码更复杂,将#include "Square.h"
替换为Square class Square;
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将指针从一个void函数传递到另一个C++
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 将指针分配给另一个指针时会发生什么情况?
- 将指向给定子类的指针从一个向量复制到另一个向量
- C++将一个指针分配给另一个指针时执行的类型检查
- 将一个类的方法指针存储到另一个类中
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- 初始化类中的指针数组,并在另一个类中检索它
- 从另一个指针指向函数的指针
- 使用指向成员函数的指针在另一个类中调用类构造函数
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
- 如果类"A"具有指向另一个类的指针,则"B" A 可以访问 B 类公共方法
- 如何使用指针从另一个类访问属性
- 如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?
- C++ 无效双指针干扰另一个
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针