0
我是新來的C++,最近我試過如下:C++:通過索引從列表中獲取對象不起作用?
list<Someclass> listofobjects;
int Index;
cin >> Index;
Someclass anobject = listofobjects[Index];
作爲輸出我得到以下錯誤:
../src/Kasse.h:98:71: error: no match for ‘operator[]’ in ‘((Someclass*)this)->Someclass::listofobjects[((Someclass*)this)->Someclass::Index]’
有誰知道爲什麼嗎?我無法找到一個解決方案...提前 感謝
顯示代碼聲明'listofobjects'是一個'Index',現在我們只能猜測它在類中的發生了什麼 – stijn
,但我已經在問題中添加了列,列表被填充到類的構造函數中。 – RandomDisplayName
std ::列表沒有運算符[]:http://en.cppreference.com/w/cpp/container/list和http://stackoverflow.com/questions/1112724/why-isnt-there-an-operator-for-a -stdlist - 如果你想索引,你需要例如向量(也不應該是'int',而是'size_t') – stijn