我的包含在名为“stats.h”的 header 中
我有 2 个类 A 和 B。
A需要B的头部,A需要B
的头部当我尝试编译时,我遇到了大约 50 个这样的错误
identifier 'Player'
int assumed
; before * missing
关于如何解决这个问题有什么想法吗? :(
(这里是相关代码)
A 级 - 玩家
#include "stats.h"
class Player
{
public:
Player(int x, int y);
Texture *texture;
int x;
int y;
float rotation;
void Update(double elapsedTime);
void Draw(Sprite *sprite);
private:
Weapon *weapon;
};
B 级 - 武器
#include "stats.h"
class Weapon // **CLASS B**
{
public:
double interval;
Weapon(Player *player);
void Update(double elapsedTime);
void Draw(Sprite *sprite);
protected:
private:
Player *player;
};
STATS.H
#include "Player.hpp"
#include "Weapon.hpp"
最佳答案
您的 header 中没有实现,因此您可以传递前向声明并仅在 cpp 文件中包含 header 。
例如-在class Player
之前,提出class Weapon
的声明:
//vvvvvvvvvvv
class Weapon;
class Player
{
public:
Player(int x, int y);
/* .. */
};
并删除 #include "stats.h"
。 class Weapon
关于C++,#include 问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5894385/