我想寫一個程序來設置一個嵌套結構,然後初始化該結構的數組。這給我一些奇怪的錯誤。這裏的所有相關代碼:如何初始化一個嵌套結構數組
//Structure called Stats for storing initial character stats
struct Stats{
string name;
int level;
int HP;
int STR;
int CON;
int DEX;
int INT;
int WIS;
int CHA;};
//Structure called Growth for storing character growth per level.
struct Growth{
int HPperlvl;
int STRperlvl;
int CONperlvl;
int DEXperlvl;
int INTperlvl;
int WISperlvl;
int CHAperlvl;};
struct Holdstats{
Stats classstats;
Growth classgrowth;};
const int SIZE = 10;
Holdstats classlist[SIZE];
Holdstats charlist[SIZE];
//Define initial classes, to be stored in the Classes structure
classlist[0].classstats = {"Fighter", 1, 18, 10, 10, 10, 10, 10, 10};
classlist[0].classgrowth = {1,1,1,1,1,1,1};
classlist[1].classstats = {"Wizard", 1, 10, 10, 10, 10, 10, 10};
classlist[1].classgrowth = {1,1,1,1,1,1,1}
我的編譯器認爲,當我鍵入「班級列表[0] .classstats」我試圖初始化大小爲0的數組我看這個問題的方法,我想訪問類列表數組的第一個元素。這寫的是否正確?
如果有人能給我一個這樣一個數組的樣子的簡短例子,那將是很棒的。從那裏我想寫它作爲一個載體
甚至沒有讀過這個問題,但是你需要在結構聲明之後有一個分號。 – gsingh2011
是的,我注意到,壞複製粘貼錯誤哈哈。 –