我正在做一个小游戏。
在 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 函数。我得到了错误并坚持到这个点。