-1

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?

Trista N
  • 403
  • 1
  • 4
  • 11

3 Answers3

3

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.

adl
  • 15,627
  • 6
  • 51
  • 65
2

You can't do that. Bullets contains Parts, 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.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
1

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"?

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167