多个文件中包含多个 [英] Multiple inclusion in multiple files

查看:157
本文介绍了多个文件中包含多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个小游戏。

在BattleRecord.h中:

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;
};

在Character.h中:

In Character.h:

#ifndef _EQUIPMENT_H_
#define _EQUIPMENT_H_
#include "Equipment.h"
#endif

class BattleRecord;
class Character
{
BattleRecord AttackEnemy(Character &Enemy);
}

在BattleRecord.h:

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)
}

Character.h和BattleRecord.h需要包含彼此,这肯定会导致多重重定义的问题。因此,我在Character.h中使用了向前声明:

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;

问题出现了。但是,GUI.h需要BattleRecord.h报告战斗,所以我必须将BattleRecord.h包含在GUI.h中。我还必须包括Character.h为了传递到ViewStat函数。

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.

推荐答案

您使用包含守卫错误。它们应该出现在您打算仅防止多个包含的文件中,它们应该覆盖整个文件。 (不只是包含)。

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).

例如,在BattleRecord.h

For example, in 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_

这篇关于多个文件中包含多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆