Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVector<T> * dont want work with operator[]
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • UndeadBlowU Offline
    UndeadBlowU Offline
    UndeadBlow
    wrote on last edited by
    #1

    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@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • UndeadBlowU Offline
        UndeadBlowU Offline
        UndeadBlow
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [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 ;-)

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved