Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]C++ - setRadius doesn't work in the new 'for' ?  (Read 1502 times)

0 Members and 1 Guest are viewing this topic.

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
[SOLVED]C++ - setRadius doesn't work in the new 'for' ?
« on: July 11, 2015, 11:30:09 pm »
What am I doing wrong?
Why does this work
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "wtf");
   
    window.setVerticalSyncEnabled(true);
   
    const unsigned int NumberOfPlayers = 4;

    sf::CircleShape Players[NumberOfPlayers];

    // Puttin the 'setRadius' inside this for makes it work
    for(unsigned int i = 0; i<NumberOfPlayers; i++)
    {
        Players[i].setRadius(50);
        Players[i].setPosition({i*100, 0});
    }
   
    while (window.isOpen())
    {
        window.clear(sf::Color::Black);
        for(auto player : Players)
            window.draw(player);
        window.display();
    }

    return 0;
}
 
and this doesn't?
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "wtf");
   
    window.setVerticalSyncEnabled(true);
   
    const unsigned int NumberOfPlayers = 4;

    sf::CircleShape Players[NumberOfPlayers];
   
    // This doesn't work!
    for(auto player : Players)
    {
        player.setRadius(50);
    }
   
    // Puttin the 'setRadius' inside this for makes it work
    for(unsigned int i = 0; i<NumberOfPlayers; i++)
    {
        Players[i].setPosition({i*100, 0});
    }
   
    while (window.isOpen())
    {
        window.clear(sf::Color::Black);
        for(auto player : Players)
            window.draw(player);
        window.display();
    }
   
    return 0;
}
 

The change is the location of 'setRadius' in the code. The second snippet results in a blank window.

Thanks
« Last Edit: July 11, 2015, 11:35:04 pm by SoleSoul »

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: C++ - setRadius doesn't work in the new 'for' ?
« Reply #1 on: July 11, 2015, 11:34:46 pm »
My bad.
Should have been
for(auto &player : Players)
 

I didn't know that.
Thanks :)