简体   繁体   English

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

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

I'm having trouble getting a global struct working.我无法让全局结构正常工作。 I'm trying to declare and define a struct called Hero in a header file and then I make instances of it in Global.cpp but then when I try to call it in Main.cpp, the visual studio does not recognize the struct or allow me to do anything with it.我试图在 header 文件中声明和定义一个名为 Hero 的结构,然后在 Global.cpp 中创建它的实例,但是当我尝试在 Main.cpp 中调用它时,Visual Studio 无法识别该结构或允许我用它做任何事情。 I need to be able to call the struct and manipulate the variables in it for a simple C++ console game I'm making.对于我正在制作的简单 C++ 控制台游戏,我需要能够调用该结构并操纵其中的变量。

I've tried to set the struct up as follows:我尝试将结构设置如下:

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

After all of the instances of the Hero struct were defined in Global.cpp, I have been unable to use anything from that struct in any other cpp file.在 Global.cpp 中定义了 Hero 结构的所有实例之后,我无法在任何其他 cpp 文件中使用该结构中的任何内容。 For example, in the Main.cpp, I want to be able to link each Character Class (Elf, Fairy, Salamander, Nymph, etc.) to the players that are playing the game and then change the values of power level, hitpoints, magic points, etc. using code.例如,在 Main.cpp 中,我希望能够将每个角色 Class(精灵、仙女、蝾螈、仙女等)链接到正在玩游戏的玩家,然后更改功率等级、生命值、魔术点等使用代码。 I need to update these values as the players are progressing through the game and achieving goals.随着玩家在游戏中取得进展并实现目标,我需要更新这些值。

So what do I need to do to make this work?那么我需要做什么才能完成这项工作? Also, if on top of that functionality, I can also create instances of the Hero struct with a loop for each player playing the game, I would be very happy with the new code.此外,如果在该功能之上,我还可以为每个玩游戏的玩家创建带有循环的 Hero 结构实例,我会对新代码感到非常满意。 I don't know what to do from here.我不知道从这里做什么。

If you really want to use global variables and use them across multiple translation units (.cpp files), you need to declare them extern so they're created once but visible everywhere.如果您真的想使用全局变量并在多个翻译单元(.cpp 文件)中使用它们,则需要将它们声明为extern ,以便它们只创建一次但随处可见。

// Global.h

struct Hero {
  // ...
};

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

Now when Global.h is #include d by main.cpp those variables are visible and main.cpp knows that they're defined elsewhere.现在,当Global.hmain.cpp#include d 时,这些变量是可见的,并且main.cpp知道它们是在其他地方定义的。 But consider rethinking your use of global variables in general as they tend to cause more problems than they solve.但是请考虑重新考虑您对全局变量的使用,因为它们往往会导致比它们解决的问题更多的问题。

You need to tell any modules that use the instances of Hero defined in the "Global.cpp" file (such as the "Main.cpp" file) that they exist and are defined somewhere (else).您需要告诉任何使用“Global.cpp”文件(例如“Main.cpp”文件)中定义的Hero实例的模块它们存在并且在某处(其他)定义。 To do this, add one or more extern Hero xxx;为此,添加一个或多个extern Hero xxx; declarations in your "Global.h" header, right after the definition of the Hero structure.在您的“Global.h”header 中的声明,就在Hero结构的定义之后。

As a one liner, this could be:作为一个班轮,这可能是:

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

You could also split the extern declarations into multiple lines, with anything from one to all of the declarations on each:您还可以将extern声明拆分为多行,每行包含从一个到所有声明的任何内容:

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

There are two options for this problem:这个问题有两种选择:

In Global.h put extern Hero and the name of every one of the Character Class.Global.h中放入extern Hero和每个字符 Class 的名称。 Ex: extern Hero Elf;例如: extern Hero Elf;

Or you can, #include Global.cpp in main.cpp and in Global.cpp add the word static before every Hero like this:或者你可以在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 };

The static makes each Hero be created once per file (so no already defined errors). static使每个英雄在每个文件中创建一次(因此没有已经定义的错误)。 When I ran this code in main.cpp :当我在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;
}

It outputted它输出了

Elf
0
5
5
1

As it should.正如它应该。

Bye!再见!

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

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