Pointer to a QList statement - at () vs. []

I have a problem understanding the behavior of QList.

#include <QList> #include <iostream> using namespace std; int main() { QList<double> *myList; myList = new QList<double>; double myNumber; double ABC; for (int i=0; i<1000000; i++) { myNumber = i; myList->append(myNumber); ABC = myList[i]; //<----------!!!!!!!!!!!!!!!!!!! cout << ABC << endl; } cout << "Done!" << endl; return 0; } 

I get a compilation error cannot convert 'QList to' double in assignment in the marked line. It works when I use ABC = myList.at(i) , but the QT link seems to say that the at() and [] operator are the same. Does anyone know what the difference is?

thanks

+6
c ++ qt qt4 qlist
source share
3 answers

This is because operator[] should be applied to the QList object, but myList is a pointer to QList .

Try

 ABC = (*myList)[i]; 

instead of this. (Also, the correct syntax should be myList->at(i) instead of myList.at(i) .)

+9
source share

You probably meant

 ABC = (*myList)[i]; 
+3
source share

myList is a pointer to a QList, so you should use it as (*myList)[i] in a line marked with exclamation points. Also, you cannot use ABC = myList.at(i) , you need to use ABC = myList->at(i)

+3
source share

All Articles