gpt4 book ai didi

C++ 通过跨多个 .cpp 文件所需的信息一次性读取一个头文件

转载 作者:行者123 更新时间:2023-11-28 07:57:07 24 4
gpt4 key购买 nike

我和我的 friend 正在一起学习 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;

我们需要在这 2 个 .cpp 文件中 #include 这个头文件(还有 2 个计划也需要访问这个类)但实际上只读取一次头文件(这样我们就可以避免读取静态变量初始化不止一次)。我们正试图避开构造函数(主要是为了便于使用静力学,也为了我们的学习目的)。我们已经阅读了有关 Header Guards 的信息,但我们无法弄清楚如何让它们解决这个问题。

我们将不胜感激您的任何提示/建议!

最佳答案

你应该只在你的头文件中声明变量(那些应该是外部的,而不是静态的,全局变量,或者带有外部链接的静态成员变量)。因此,请保持静态成员变量声明(在您的 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 guards在您的头文件中,它以

开头
#ifndef PLAYER_STATISTICS_INCLUDED
#define PLAYER_STATISTICS_INCLUDED

结尾
#endif /*PLAYER_STATISTICS_INCLUDED*/

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

关于C++ 通过跨多个 .cpp 文件所需的信息一次性读取一个头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12453118/

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