2012-06-20 34 views
0

我正在準備一些代碼,以便最終成爲MUD;這是我的第一個'大'項目,我正逐漸挑出錯誤;然而,現在有些問題阻礙了我的項目,我似乎無法打破它們。這裏是我的代碼:結構內的結構數組,C++代碼

#include <iostream> 
using namespace std; 

int test_var; 
#define K 125 
#define TEST 50 

struct item { 
int quantity; 
//Some More Stuff Will Be Inside Later// 
}; 

struct inventory { 
    struct item[K]; //Error 1 - "expected unqualified-id before '[' token" 
} test; 

int main() 
{ 
cout << "Number?" << endl; 
cin >> test_var; 
test.item[TEST].quantity = test_var; //Error 2 - "'struct inventory' has no member named 'item'" 
cout << test.item[TEST].quantity << endl; //Error 3 - "'struct inventory' has no member named 'item'" 
cout << test.item[TEST].quantity; //Error 4 - "'struct inventory' has no member named 'item'" 
return 0; 
} 

還向我道歉,因爲這代碼是有點草率,但是這代表了我試圖完成兩級件事的任務。數字1,我​​需要在結構「庫存」內部有一些結構「物品」的方法。 2號,我需要確保我可以訪問結構內的各個元素;實際的代碼在結構中涉及更多的結構,並且能夠訪問單個非結構元素(ints,bools,double,strings)是非常重要的。如果有人能就這些問題提供許多建議,我將不勝感激。謝謝

+0

結構項目[k]中的第一個錯誤是你沒有給這個項目一個名字。試試這個:struct item name [k] –

回答

3
struct item[K]; 

您缺少結構的對象的標識符/名稱。請注意0​​本身就是一個結構體。所以,試試

struct item obj[K]; // struct key word is unnecessary