PDA

View Full Version : Qvector warnings 'read only structure'.



tonnot
13th July 2011, 09:46
I have :

QVector<W3D_WORLD> *mw3d = new QVector<W3D_WORLD>;
W3D_WORLD *w3d_world = new W3D_WORLD;
mw3d->push_back(*w3d_world);
mw3d->at(i).w3d_ng++;

When compiled I have :
increment of data-member 'W3D_WORLD::w3d_ng' in read-only structure

But it works using STL vector ???

vector<W3D_WORLD> *mw3d = new vector<W3D_WORLD>;

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

mcosta
13th July 2011, 09:49
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

mvuori
13th July 2011, 09:59
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".

tonnot
13th July 2011, 10:04
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

mcosta
13th July 2011, 10:27
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)