2013-03-06 25 views
-2

添加對象我有以下的Structs:在數組C++

struct Photo{ 
    char name[30];   
}; 

struct List { 
    Element *data; 
    int count; 
    int capacity; 
}; 

typedef Photo* Element;

另外,我有類型的「列表」的數組:List *list = new List[100];和類型的「照片」的元素: Photo *e = new Photo;

我想在數組列表的開頭插入元素「* e」。我正在使用以下C++語句: list -> data[0] = e;

但是,我結束了Segmentation fault: 11錯誤。

我用C++新的,我需要知道我怎麼可以保存元素「E」中的數組「列表」

+4

[使用std :: vector](http://en.cppreference.com/w/cpp/container/vector) – StoryTeller 2013-03-06 21:27:26

+3

你有沒有考慮過使用'std :: vector'? – JBentley 2013-03-06 21:27:45

+0

'std :: vector '也可能使用'std :: string'作爲名字。 – 111111 2013-03-06 21:28:55

回答

4

list -> data[0] = e;之初沒有定義。

什麼你可能打算做的是list[0].data = e;

+0

「l」是「list」。我編輯 – Programmer 2013-03-06 21:32:14

+0

更正。想指出一個std :: vector或std :: list可能是一個不太容易出錯的解決方案。但是,再一次,只有你知道該計劃最終是要支持的。 – fredrik 2013-03-06 21:34:09

1

我用C不++編寫代碼,但如果我的理解,你想給照片添加到列表的數組。您必須將List僅添加到您的List數組中。