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

10,803

Solution 1

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

Solution 2

You've probably meant

ABC = (*myList)[i];

Solution 3

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)

Share:
10,803
Moomin
Author by

Moomin

Updated on June 18, 2022

Comments

  • Moomin
    Moomin almost 2 years

    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

  • Moomin
    Moomin over 14 years
    yes I meant myList->at(i) - mistyping in question, thanks for help.