gpt4 book ai didi

c++ - 多个包含在多个文件中

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:10:11 25 4
gpt4 key购买 nike

我正在制作一个小游戏。

在 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_

关于c++ - 多个包含在多个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5106650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com