0

how come the i.X and i.Y are not being updated in the fb.Entities collection?

am I doing something wrong? I'm learning, Is this the correct way to update values of something in a vector?

    for (Entity i : fb.Entities) 
    {
        if (i.Serial == SerialID)
        {
            i.X = (USHORT)((data[5] << 8) + data[6]);
            i.Y = (USHORT)((data[7] << 8) + data[8]);
            break;
        }
    }
Dean
  • 499
  • 6
  • 13
  • 34

2 Answers2

3

You need

for (Entity& i : fb.Entities) 

to take the actual entity, not a copy.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

You need to take references to the container elements:

for (Entity & i : fb.Entities)  { /* ... */ }
//         ^^^

Alternatively:

for (auto & i : fb.Entities)

(Otherwise, i will be a copy of the container element, and so any changes to it are lost.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084