lvalue required as left operand of assignment
I am getting the above error when I try to compile this piece of code:
Declaration of bullets
:
Where am I going wrong?
To explain the error message: The compiler complains that &(*i) = ...
makes no sense: &(*i)
is an address and cannot be changed.
For the fix, see fontanini's answer.
You can't do that. Bullets contains Part
s, not Part*
. You also can't modify the memory address of the Part
objects stored in your list, you can modify their contents.
You could use a list of pointers:
std::list<Part*> bullets;
/* ... */
*i = new BrokenPart(**i);
Note the two derreference operators on the iterator, once to get the Part
pointer, and then another to derreference it.
As an advice, i would tell to to change your design. It looks like that Part
class could be redesigned using the state pattern, you should have a look at it.
If you want to construct new BrokenPart
on a pre-allocated memory where object that i
points to is stored, you could use placement new operator: new (&(*i)) BrokenPart();
Check out this question: What uses are there for "placement new"?