Pointer to QList - at() vs. [] operator

Moomin picture Moomin · Feb 9, 2010 · Viewed 10.6k times · Source

I'm having problem with understanding some of QList behavior.

#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 compilation error cannot convert ‘QList’ to ‘double’ in assignment at marked line. It works when I use ABC = myList.at(i), but QT reference seems to say that at() and [] operator is same thing. Does anybody know what makes the difference?

Thanks

Answer

kennytm picture kennytm · Feb 9, 2010

That's because operator[] should be applied to a QList object, but myList is a pointer to QList.

Try

ABC = (*myList)[i];

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