QVector<T> * dont want work with operator[]



  • Hello everyone. Please, help me to understand, why when I'm have, for example
    @QVector<double> *p = new QVector<double>()@

    Then this will't work (because compiler will scream something like " no match for 'operator=' (operand types are 'QVector<double>' and 'double' ")
    @p[0] = 0.0@
    But this will:
    @p->operator = 0.0@


  • Moderators

    This is the proper way to use QVector as an array.
    @
    QVector<double> vec(10);
    vec[0] = 0.0; // uses the overloaded operator[]
    // vec is a container
    @

    You can use:
    @
    double *p = new double[10];
    p[0]= 0.0; // you use the access operator with an offset
    // p is a pointer to double. Sou you can assign a double as
    // above
    @

    What you did:
    @
    QVector<double> *p; // this is a pointer to an object of QVector<double>
    p = new QVector<double>(); // you allocate the memory of the object.

    // p[0] = // you access an object of QVector with offset 0, but
    //
    QVector &vec = *p; // this should work
    vec = p[0]; // you assign the whole vector to vec
    vec[0] = 0.0; // this will work ; you assign 0.0 to the first element of
    // vector
    (*p)[0] = 0.0; // this should also work

    @

    The answer to your question is that the compiler recognizes your construct as an offset of a pointer, but the indication of one element in the vector is still missing.

    Are you sure that this is working?
    @
    p.operator = 0.0;
    @

    It should look more like
    @
    p->operator = 0.0;
    @

    but I did not check it out.



  • Oooh, now I'm understand. Thank you very much.
    [quote]Are you sure that this is working?[/quote]
    Of couse I'm mean ->, just written by hand and do mistake.


  • Moderators

    [quote author="UndeadDragon" date="1383328028"]Oooh, now I'm understand. Thank you very much.
    [/quote]
    You are welcome!
    [quote author="UndeadDragon" date="1383328028"]
    Of couse I'm mean ->, just written by hand and do mistake.[/quote]
    I thought so ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.