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.
extern Hero Elf;in the header file.