繁体   English   中英

如何在我的 c++ 程序中初始化和使用全局结构?

[英]How do I initialize and use a global struct in my c++ program?

我无法让全局结构正常工作。 我试图在 header 文件中声明和定义一个名为 Hero 的结构,然后在 Global.cpp 中创建它的实例,但是当我尝试在 Main.cpp 中调用它时,Visual Studio 无法识别该结构或允许我用它做任何事情。 对于我正在制作的简单 C++ 控制台游戏,我需要能够调用该结构并操纵其中的变量。

我尝试将结构设置如下:

//Global.h
#pragma once
#include <iostream>
#include <string>

using namespace std; 

struct Hero {
    unsigned int heroID;
    string className;
    unsigned int powerLevel;
    unsigned int HitPoints;
    unsigned int MagicPoints;
};

//Global.cpp
#include <iostream>

#include "Global.h"

Hero Elf{ 0, "Elf", 1, 5, 5 };
Hero Fairy{ 1, "Fairy", 1, 5, 5 };
Hero Salamander{ 2, "Salamander", 1, 5, 5 };
Hero Chimaera{ 3, "Chimaera", 1, 5, 5 };
Hero Nymph{ 4, "Nymph", 1, 5, 5 };
Hero Centaur{ 5, "Centaur", 1, 5, 5 };
Hero Dwarf{ 6, "Dwarf", 1, 5, 5 };
Hero Pegasus{ 7, "Pegasus", 1, 5, 5 };
Hero Griffin{ 8, "Griffin", 1, 5, 5 };
Hero Phoenix{ 9, "Phoenix", 1, 5, 5 };
Hero Troll{ 10, "Troll", 1, 5, 5 };

//Main.cpp
#include <iostream>
#include <string>
#include "Global.h"

using namespace std;

int main() {
    // HERE is where I really want to do stuff with each instance of the Hero struct, such as displaying the character stats for each player's chosen class.
    return 0;
}

在 Global.cpp 中定义了 Hero 结构的所有实例之后,我无法在任何其他 cpp 文件中使用该结构中的任何内容。 例如,在 Main.cpp 中,我希望能够将每个角色 Class(精灵、仙女、蝾螈、仙女等)链接到正在玩游戏的玩家,然后更改功率等级、生命值、魔术点等使用代码。 随着玩家在游戏中取得进展并实现目标,我需要更新这些值。

那么我需要做什么才能完成这项工作? 此外,如果在该功能之上,我还可以为每个玩游戏的玩家创建带有循环的 Hero 结构实例,我会对新代码感到非常满意。 我不知道从这里做什么。

如果您真的想使用全局变量并在多个翻译单元(.cpp 文件)中使用它们,则需要将它们声明为extern ,以便它们只创建一次但随处可见。

// Global.h

struct Hero {
  // ...
};

extern Hero Elf;
extern Hero Fairy;
extern Hero Salamander;
// ...
}

现在,当Global.hmain.cpp#include d 时,这些变量是可见的,并且main.cpp知道它们是在其他地方定义的。 但是请考虑重新考虑您对全局变量的使用,因为它们往往会导致比它们解决的问题更多的问题。

您需要告诉任何使用“Global.cpp”文件(例如“Main.cpp”文件)中定义的Hero实例的模块它们存在并且在某处(其他)定义。 为此,添加一个或多个extern Hero xxx; 在您的“Global.h”header 中的声明,就在Hero结构的定义之后。

作为一个班轮,这可能是:

extern Hero Elf, Fairy, Salamander, Chimaera, Nymph, Centaur, Dwarf, Pegasus, Griffin, Phoenix, Troll;

您还可以将extern声明拆分为多行,每行包含从一个到所有声明的任何内容:

extern Hero Elf;
extern Hero Fairy, Salamander;
//...

这个问题有两种选择:

Global.h中放入extern Hero和每个字符 Class 的名称。 例如: extern Hero Elf;

或者你可以在main.cpp和 Global.cpp 中的#include Global.cpp Global.cpp添加单词static在每个Hero之前,如下所示:

//Global.cpp
#include "Global.h"

static Hero Elf{ 0, "Elf", 1, 5, 5 };
static Hero Fairy{ 1, "Fairy", 1, 5, 5 };
static Hero Salamander{ 2, "Salamander", 1, 5, 5 };
static Hero Chimaera{ 3, "Chimaera", 1, 5, 5 };
static Hero Nymph{ 4, "Nymph", 1, 5, 5 };
static Hero Centaur{ 5, "Centaur", 1, 5, 5 };
static Hero Dwarf{ 6, "Dwarf", 1, 5, 5 };
static Hero Pegasus{ 7, "Pegasus", 1, 5, 5 };
static Hero Griffin{ 8, "Griffin", 1, 5, 5 };
static Hero Phoenix{ 9, "Phoenix", 1, 5, 5 };
static Hero Troll{ 10, "Troll", 1, 5, 5 };

static使每个英雄在每个文件中创建一次(因此没有已经定义的错误)。 当我在main.cpp中运行此代码时:

//Main.cpp
#include <iostream>
#include "Global.cpp"
#include "Global.h"
using namespace std;

int main() {
    Hero player1;
    player1 = Elf;
    cout << player1.className<<endl;
    cout << player1.heroID<<endl;
    cout << player1.HitPoints<<endl;
    cout << player1.MagicPoints<<endl;
    cout << player1.powerLevel<<endl;
    return 0;
}

它输出了

Elf
0
5
5
1

正如它应该。

再见!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM