尝试使用自定义对象 class 时,字段变量声明无效错误
variable of field declared void error when trying to use object of custom class
我的项目中有这个头文件:
#ifndef COMMONINCLUDES_H
#define COMMONINCLUDES_H
#include "utilities.h"
#include "textOutput.h"
#include "game.h"
class Player
{
public:
int score;
int poX, posY;
};
#endif // COMMONINCLUDES_H
然后在另一个头文件中我有
#include <commonincludes.h>
void turn(Player *currplayer, int mapSize);
和它自己的 cpp 文件中的匹配函数。我在函数原型行收到一个声明为 void 的字段变量错误,尽管包含头文件,但未在此范围内声明 'Player' 的错误。在这里查看一堆关于错误的其他线程,我还没有找到一个不是针对一个文件中的所有代码或由我已经排除的错误引起的错误。我知道我以前做过类似的事情并让它发挥作用,但我无法弄清楚这次我做了什么不同的事情。
我还把 class 定义(复制粘贴所以我知道我没有用 class 定义更改任何内容)移动到头文件 "utilities.h"包含在上面的头文件中使其工作...
如果这还不够,请告诉我,我会添加更多内容。
您有循环 header 包含。 game.h
包括 commonincludes.h
,commonincludes.h
包括 game.h
。您的包含守卫打破了无限包含循环,但他们在 Player
在 turn
声明时仍未声明的地方打破了它。
从您目前发布的内容来看,commonincludes.h
中不需要 #include "game.h"
。它在那里做什么?
P.S。使用 #include "..."
包含您自己的 header 文件。 #include <...>
语法适用于标准 headers.
我的项目中有这个头文件:
#ifndef COMMONINCLUDES_H
#define COMMONINCLUDES_H
#include "utilities.h"
#include "textOutput.h"
#include "game.h"
class Player
{
public:
int score;
int poX, posY;
};
#endif // COMMONINCLUDES_H
然后在另一个头文件中我有
#include <commonincludes.h>
void turn(Player *currplayer, int mapSize);
和它自己的 cpp 文件中的匹配函数。我在函数原型行收到一个声明为 void 的字段变量错误,尽管包含头文件,但未在此范围内声明 'Player' 的错误。在这里查看一堆关于错误的其他线程,我还没有找到一个不是针对一个文件中的所有代码或由我已经排除的错误引起的错误。我知道我以前做过类似的事情并让它发挥作用,但我无法弄清楚这次我做了什么不同的事情。
我还把 class 定义(复制粘贴所以我知道我没有用 class 定义更改任何内容)移动到头文件 "utilities.h"包含在上面的头文件中使其工作...
如果这还不够,请告诉我,我会添加更多内容。
您有循环 header 包含。 game.h
包括 commonincludes.h
,commonincludes.h
包括 game.h
。您的包含守卫打破了无限包含循环,但他们在 Player
在 turn
声明时仍未声明的地方打破了它。
从您目前发布的内容来看,commonincludes.h
中不需要 #include "game.h"
。它在那里做什么?
P.S。使用 #include "..."
包含您自己的 header 文件。 #include <...>
语法适用于标准 headers.