简体   繁体   中英

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. I need to be able to call the struct and manipulate the variables in it for a simple C++ console game I'm making.

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. 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. 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. 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.

// 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. 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). To do this, add one or more extern Hero xxx; declarations in your "Global.h" header, right after the definition of the Hero structure.

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 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. Ex: 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:

//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). When I ran this code in 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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