开发者

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

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

#include <QList>
#include <iostream>
using nam开发者_JAVA百科espace 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


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


You've probably meant

ABC = (*myList)[i];


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜