C++一次读取头文件,尽管多个.cpp文件需要信息

C++ Reading A Header File One Time Though Info Required Across Multiple .cpp Files

本文关键字:文件 cpp 信息 一次 读取 C++      更新时间:2023-10-16

我和我的朋友正在一起学习C++,我们有一个问题。我们已经创建了一个头文件,其中包含一个使用静态变量和静态函数的类,这些函数和变量总共需要对2+.cpp文件进行访问,但由于我们正在初始化头中的静态变量,所以头文件只能读取一次(由于变量的静态初始化)。

我们尝试在MainFunction.cpp文件中只包含头文件,但如果我们这样做,其他.cpp文件就无法使用静态变量和函数。将头文件包含在(当前)两个需要函数/变量的.cpp文件中会导致大量链接错误,因为静态变量被多次初始化。

这是包含类和静态变量初始化的头文件:

//PlayerStatistics.h
// Classes
class PlayerStatistics
{
public:
// Functions To Change Player Statistics
static void SetStats(short int HitPoints, short int MagickaPoints, short int Fatigue, short int Damage, short int Defense, short int Dodge, short int Block, short int SpellCastChance);
static void SetLevel(short int Experience, short int Level);
static void SetExperience(short int Experience);
// Player Statistics
static short int HitPoints;
static short int MagickaPoints;
static short int Fatigue;
static short int Damage;
static short int Defense;
// Chance Based System Player Statistics (Relies on Fatigue Level)
static short int Dodge;
static short int Block;
static short int SpellCastChance;
static short int Experience;
static short int Level;
};
// STATIC VARIABLES
short int PlayerStatistics::HitPoints = 20;
short int PlayerStatistics::MagickaPoints = 20;
short int PlayerStatistics::Fatigue = 20;
short int PlayerStatistics::Damage = 20;
short int PlayerStatistics::Defense = 20;
short int PlayerStatistics::Dodge = 20;
short int PlayerStatistics::Block = 20;
short int PlayerStatistics::SpellCastChance = 20;
short int PlayerStatistics::Experience = 0;
short int PlayerStatistics::Level = 1;

以及需要访问的2.cpp文件:

1:

//PlayerCharcter.cpp
void PlayerStatistics::SetLevel(short int Experience, short int Level)
{
Experience = Experience;
Level = Level;
}
void PlayerStatistics::SetStats(short int HitPoints, short int MagickaPoints, short int Fatigue, short int Damage, short int Defense, short int Dodge, short int Block, short int SpellCastChance)
{
HitPoints = HitPoints;
MagickaPoints = MagickaPoints;
Fatigue = Fatigue;
Damage = Damage;
Defense = Defense;
Dodge = Dodge;
Block = Block;
SpellCastChance = SpellCastChance;
}
void PlayerStatistics::SetExperience(short int Experience)
{
Experience = PlayerStatistics::Experience;
}
void AddExperience()
{
// This is a Testing Function to test the Level Up system and should be removed once Creatures
// are added to the game, or it can be edited to include proper experience points given for
// killing curtain enemies.
short int GetExperienceNumber;
std::cout << "How many experience points do you want? n";
std::cin >> GetExperienceNumber;
PlayerStatistics::Experience = (PlayerStatistics::Experience += GetExperienceNumber);
std::cout << GetExperienceNumber << " Experience Points Added! n n n n n";
}

2:

//WordBank.cpp
std::cout << "n n n n n n n n n n n n"
<< PlayerName << "'s Character Sheat! n n n"
<< "Level: " << PlayerStatistics::Level << "n"
<< "Experience: " << PlayerStatistics::Experience << "n n n"
<< "Health: " << PlayerStatistics::HitPoints << "n"
<< "Magicka: " << PlayerStatistics::MagickaPoints << "n"
<< "Fatigue: " << PlayerStatistics::Fatigue << "n"
<< "Attack: " << PlayerStatistics::Damage << "n"
<< "Defense: " << PlayerStatistics::Defense << "n"
<< "Dodge Skill: " << PlayerStatistics::Dodge << "n"
<< "Block Skill: " << PlayerStatistics::Block << "n"
<< "Spell Casting Skill: " << PlayerStatistics::SpellCastChance << "n"
<< "n n n n n n n" << "To Continue Type Any Key In And Hit Enter" << std::endl;

我们需要在这两个.cpp文件中#包括这个头文件(计划另外两个文件也需要访问这个类),但实际上只读取一次头文件(这样我们就可以避免多次读取静态变量初始化)。我们试图避开构造函数(主要是为了便于使用静态,但也为了我们的学习目的)。我们已经读过关于头部防护的文章,但我们不知道如何让它们解决这个问题。

我们将非常感谢您的任何提示/建议!

您可能应该只在头文件中声明变量(这些变量应该是外部的,而不是静态的、全局的或具有外部链接的静态成员变量)。因此,保持静态成员变量声明,如(在PlayerStatistics.h头文件中)

class PlayerStatistics {
/// ....
static short int HitPoints;
};

(它是一个带有外部链接的静态成员变量声明)

然后你可以把定义像

short int PlayerStatistics::HitPoints = 20;
short int PlayerStatistics::MagickaPoints = 20;
short int PlayerStatistics::Fatigue = 20;

在一个单独的实现文件中(通常通常是包含main(例如一些main.cpp)的文件)。

你可能应该在你的头文件中放入include guard,也就是用启动它

#ifndef PLAYER_STATISTICS_INCLUDED
#define PLAYER_STATISTICS_INCLUDED

并以结束

#endif /*PLAYER_STATISTICS_INCLUDED*/

我强烈建议你研究一些现有的C++自由软件应用程序的源代码(例如http://freecode.com/或其他免费软件集合)。这将教会你很多。