[英]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.h
是main.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.