【发布时间】:2014-02-11 01:27:44
【问题描述】:
我是 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的代码,现在我们只能猜测发生了什么 -
它在一个类中,但我在问题中添加了列,列表被填充到类的构造函数中......
-
std::list has no operator []: en.cppreference.com/w/cpp/container/list and stackoverflow.com/questions/1112724/… - 如果你想要索引,你需要例如向量(而且该索引不应该是
int,而是size_t) -
QList 有 [] 运算符 :) qt-project.org/doc/qt-5.0/qtcore/qlist.html
-
@otisonoza:确实,
QList比std::list更接近std::deque。QLinkedList更像std::list。但是这个问题(大概)是关于标准库的。