多个文件中的多次包含
我正在制作一个小游戏。
在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 函数。我遇到了错误并一直坚持到现在。
I am making a small game.
In 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;
};
In Character.h:
#ifndef _EQUIPMENT_H_
#define _EQUIPMENT_H_
#include "Equipment.h"
#endif
class BattleRecord;
class Character
{
BattleRecord AttackEnemy(Character &Enemy);
}
In 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)
}
The problem here is, my Character.h and BattleRecord.h need to include each other, and this definitely will cause multiple redefinition problem. Therefore, I used forward declaration in Character.h by adding:
class BattleRecord;
The problem is sovled. But then, the GUI.h needs BattleRecord.h again for reporting the battle, so I have to include BattleRecord.h into the GUI.h. I also have to include the Character.h in order to pass into the ViewStat function. I got error and stuck with this up to this piont.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您错误地使用了包含守卫。它们应该出现在您打算仅防止多次包含的文件中,并且它们应该覆盖整个文件。 (不仅仅是包含)。
例如,在 BattleRecord.h 中
You're using inclusion guards wrong. They should appear in the file that you intend to prevent multiple inclusions only, and they should cover the entire file. (not just the includes).
For example, in BattleRecord.h
将
#endif
放在文件末尾,而不是包含的末尾,或者在顶部使用#pragma Once
(如果您的编译器支持此操作,尽管这样可移植性较差)。编辑:
进一步解释什么是
#ifdef
&ifndef
的作用是告诉编译器在编译中完全包含或排除代码。您想要这样做的原因是因为包含头文件基本上是在说“将所有代码放入此文件中”,如果您多次这样做,那么在最佳情况下,您会因重新定义对象而出现命名冲突,并且编译时间会变慢。
Put your
#endif
at the end of the file not the end of your includes or use#pragma once
at the top if your compiler supports this although that is less portable.Edit:
To further explain what
#ifdef
&ifndef
does is tell the compiler to include or exclude code entirely from compilation.The reason you want to do this is because including a header file is basically saying "put all the code in this file here" if you did that multiple times then you'd have naming conflicts from redefining objects and slow compile times in the best scenario.
一般来说,使用前向声明而不是包含。这可以最大限度地减少包含文件包含的包含数量。唯一的例外是,当您定义的类是派生类时,您需要包含基类。
In general, use forward declaration instead of includes. This minimizes how many includes you include file contains. The only exception is when the class you are defining is a derived class, then you need to include the base class.
除了上面提到的包含防护问题(您还有 _CHARACTER_H_/_CHARACTER_H_ 不匹配,这可能会导致 GUI.h 第 2 行出现问题),您可能需要修改对象设计,以便角色不会 AttackEnemy(),而是有一个 Battle() 类,其中引用两个角色并在战斗后生成一个 BattleRecord。这将防止角色类从一开始就必须了解 BattleRecords,并允许将来进行多角色战斗、多回合战斗或通过继承 Battle 类进行特殊战斗的可能性。
In addition to the include guard issues mentioned above (you also have a _CHARACTEr_H_/_CHARACTER_H_ mismatch that might cause you trouble on line 2 of GUI.h), you may want to revise your object design so that the Character does not AttackEnemy(), but rather there is a Battle() class where two Characters are referenced and a BattleRecord is produced after the battle. This would prevent the Character class from ever having to know about BattleRecords in the first place, would allow for the possibility of multi-Character battles in the future, having multi-turn battles, or having special battles through inheritance of the Battle class.
好的大家,
谢谢你帮助我。我已经按照建议重写了头文件的所有包含内容,现在它可以完美运行。花了相当多的时间,因为我在很多课上都做错了。
OK everyone,
Thanks for helping me. I have rewritten all the inclusions for the header files as suggested, and it works flawless now. Take quite time a bit of time since I did it wrong for many classes.