Results 1 to 5 of 5

Thread: Qvector warnings 'read only structure'.

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Qvector warnings 'read only structure'.

    I have :
    Qt Code:
    1. QVector<W3D_WORLD> *mw3d = new QVector<W3D_WORLD>;
    2. W3D_WORLD *w3d_world = new W3D_WORLD;
    3. mw3d->push_back(*w3d_world);
    4. mw3d->at(i).w3d_ng++;
    To copy to clipboard, switch view to plain text mode 
    When compiled I have :
    increment of data-member 'W3D_WORLD::w3d_ng' in read-only structure

    But it works using STL vector ???
    Qt Code:
    1. vector<W3D_WORLD> *mw3d = new vector<W3D_WORLD>;
    To copy to clipboard, switch view to plain text mode 

    W3D_WORLD is a simple structure .
    I dont understand this message. (And... it happens with every structure....)
    Any idea ?
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qvector warnings 'read only structure'.

    From Qt Docs QVector::at() return a const reference to the item.

    So it's a "read only" value.

    use operator[] to obtain a non-const object
    Last edited by mcosta; 13th July 2011 at 09:51. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Qvector warnings 'read only structure'.

    Even simple structures can have problems -- especially if they are so simple that they are not considered worth of showing to others.

    What the compiler often means with that kind of message is that you try to modify something that is "const".

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qvector warnings 'read only structure'.

    ups.
    Ok, If I use :
    mw3d->value(i).w3d_ng++; it works
    But, to get it using [] ( I'm unable to write the correct code )
    mw3d[i].w3d_ng++; fails ( error : no w3d_ng member )
    I 'd have to use [], that returns a reference (but I dont know how to write the code... )

    (*mw3d)[i]. Is the way !! Auto-answered.


    Whats mean 'Note that using non-const operators can cause QVector to do a deep copy.'?
    Whats happen at every Qvector[i] ?

    If my case mw3d->push_back(*w3d_world); Iam storing pointers ins`t it ? So , what kind of copy are going to be made ?

    STL vector .at() does not work as Qvector. What about the copy ? (It returns references , isn`t it ?)
    Excuse me, I am at a very important point on my progr. and I'd want to write the correct code. (avoiding unnecessary copies and let the program be faster)

    Thanks
    Last edited by tonnot; 13th July 2011 at 10:27.

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qvector warnings 'read only structure'.

    Whats mean 'Note that using non-const operators can cause QVector to do a deep copy.'?
    Whats happen at every Qvector[i] ?
    Qt Containters use Implicit Sharing. When you need a non-const reference it needs a deep copy

    If my case mw3d->push_back(*w3d_world); Iam storing pointers ins`t it ? So , what kind of copy are going to be made ?
    Wrong, you're storing the value of pointer (*w3d_world)
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 12
    Last Post: 26th June 2011, 11:26
  2. Deprecated warnings still appear
    By Caius Aérobus in forum Qt Programming
    Replies: 0
    Last Post: 29th March 2010, 08:25
  3. Read Text file using structure..
    By umulingu in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2009, 11:22
  4. Crosscompiling warnings
    By SteM in forum Installation and Deployment
    Replies: 1
    Last Post: 2nd August 2007, 20:26
  5. Makefile warnings
    By Doug Broadwell in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 19:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.