我正在制作一个小游戏。
在BattleRecord.h中:
#ifndef _CHARACTER_H_
#define _CHARACTER_H_
#include "Character.h"
#endif
class BattleRecord
{
public:
Character Attacker;
Character Defender;
Status status;
int DamageDealt;
int GoldEarned;
int ExpGained;
};
在Character.h中:
#ifndef _EQUIPMENT_H_
#define _EQUIPMENT_H_
#include "Equipment.h"
#endif
class BattleRecord;
class Character
{
BattleRecord AttackEnemy(Character &Enemy);
}
在BattleRecord.h中:
#ifndef _CHARACTER_H_
#define _CHARACTEr_H_
#include "Character.h"
#endif
#ifndef _BATLE_RECORD_H_
#define _BATLE_RECORD_H_
#include "BattleRecord.h"
#endif
class GUI
{
public:
//GUI Methods, and two of these:
void ViewStats(Character &Player);
void Report(BattleRecord Record)
}
这里的问题是,我的Character.h和BattleRecord.h需要相互包含,这肯定会导致多次重定义问题。因此,我在 Character.h 中添加了前向声明:
class BattleRecord;
问题解决了。但是,GUI.h 再次需要 BattleRecord.h 来报告战斗,所以我必须将 BattleRecord.h 包含到 GUI.h 中。我还必须包含 Character.h 才能传递到 ViewStat 函数。我遇到了错误并一直坚持到现在。
您错误地使用了包含守卫。它们应该出现在您打算仅防止多次包含的文件中,并且它们应该覆盖整个文件。 (不仅仅是包含)。
例如,在 BattleRecord.h 中
#ifndef _BATTLE_H_
#define _BATTLE_H_
#include "Character.h"
class BattleRecord
{
public:
Character Attacker;
Character Defender;
Status status;
int DamageDealt;
int GoldEarned;
int ExpGained;
};
#endif // _BATTLE_H_
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)