C++11 for loop syntax not working properly

c++, c++11, for-loop, foreach

Solution

The problem is that in the loop `powerUp` is a copy from the `mPowerUps` container. You need to use references:

for (auto& powerUp : mPowerUps)
    powerUp.update(mFT);

Problem

I'm making an Arkanoid clone using SFML as a learning exercise. For the PowerUp class, I have an update method like this: ``` void PowerUp::update(FrameTime mFT) { if (!(bottom() >= windowHeight)) shape.move(velocity * mFT); else onGround = true; } ``` In my PowerUpManager class, I have a PowerUp vector, and this function to update all PowerUp's at once: ``` void PowerUpManager::update(FrameTime mFT) { for (auto powerUp : mPowerUps) powerUp.update(mFT); } ``` However, this does not seem to work. If I run this code, the powerups just stay still after spawning and do not move downwards, like they should. However, if I replace ``` for (auto powerUp : mPowerUps) powerUp.update(mFT); ``` with the more traditional but older fashioned ``` for (int i{ 0 }; i < mPowerUps.size(); ++i) mPowerUps[i].update(mFT); ``` , everything works fine and as it should. This tells me that there is a difference in the meaning of those 2 bits of code. As far as I was aware, the former was simply the new C++ syntactic sugar version of the latter. Is there something I'm missing? What's even stranger is that my draw all function, ``` void PowerUpManager::draw(sf::RenderWindow& window) { for (auto powerUp : mPowerUps) powerUp.draw(window); } ``` , works fine and with no issues.

Original source