SFML community forums

Help => Graphics => Topic started by: SoleSoul on July 11, 2015, 11:30:09 pm

Title: [SOLVED]C++ - setRadius doesn't work in the new 'for' ?
Post by: SoleSoul 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
Title: Re: C++ - setRadius doesn't work in the new 'for' ?
Post by: SoleSoul on July 11, 2015, 11:34:46 pm
My bad.
Should have been
for(auto &player : Players)
 

I didn't know that.
Thanks :)