0

我的朋友和我正在一起学习 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 的信息,但我们无法弄清楚如何让它们解决这个问题。

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

4

1 回答 1

1

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

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

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

然后你可以把定义像

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

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

您可能应该在头文件中放置包含保护,即以

#ifndef PLAYER_STATISTICS_INCLUDED
#define PLAYER_STATISTICS_INCLUDED

并以

#endif /*PLAYER_STATISTICS_INCLUDED*/

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

于 2012-09-17T04:30:44.223 回答