tags:

views:

241

answers:

3

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

+2  A: 

You've probably meant

ABC = (*myList)[i];
Alexander Poluektov
+3  A: 

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).)

KennyTM
yes I meant myList->at(i) - mistyping in question, thanks for help.
Moomin
A: 

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

erelender