SFML community forums

Help => Graphics => Topic started by: Bonediggerninja on June 10, 2017, 12:06:15 pm

Title: Shape Functions not working in classes.
Post by: Bonediggerninja on June 10, 2017, 12:06:15 pm
#include <SFML/Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>




class player {
public:
        int the = 5; //Works
        sf::Font font; //Works
        sf::RectangleShape shape(sf::Vector2f(50, 50)); //Does not
        sf::CircleShape circle(50); //Does not
};
int main()
{
        sf::CircleShape circle2(50); //Does

 
It gives me the error "Function definition for ( shape name, not class) not found."
I don't think this is a sfml linking problem, but I could be wrong.
I am using VS 2015, If that matters.
Thanks in advance.
Title: Re: Shape Functions not working in classes.
Post by: eXpl0it3r on June 10, 2017, 01:24:11 pm
It's not any issue with anything other than your own understanding of C++. ;)
If you want to initialize member variables "in-class", you need to use the curly brackets or make an assignment.
Title: Re: Shape Functions not working in classes.
Post by: jerryd on June 11, 2017, 05:38:59 am
Bonediggerninja,

 I don't know exactly what you are trying to do but initializing the
 shapes should be done with the object not inside the class.
 
 This will compile:

#include <SFML/Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>

class player {
public:
        int the = 5; //Works
        sf::Font font; //Works
        sf::RectangleShape shape;
        sf::CircleShape circle;
};
int main()
{
    player player1;
    player1.circle.setRadius(50);
    player1.shape.setSize(sf::Vector2f(50, 50));
       
}
 

Title: Re: Shape Functions not working in classes.
Post by: Hapax on June 11, 2017, 11:31:09 am
You can also initialise members in a constructor:
class player {
public:
        int the;
        sf::Font font;
        sf::RectangleShape shape;
        sf::CircleShape circle;
        player()
        {
            the = 5;
            shape.setSize(sf::Vector2f(50, 50));
            circle.setRadius(50.f);
        }
};
or directly in its initialisation list:
class player {
public:
        int the;
        sf::Font font;
        sf::RectangleShape shape;
        sf::CircleShape circle;
        player()
            : the(5)
            , font()
            , shape(sf::Vector2f(50, 50))
            , circle(50)
        {
        }
};
Title: Re: Shape Functions not working in classes.
Post by: Hiura on June 14, 2017, 11:15:55 am
Just to clarify what eXpl0it3r said, you can write sf::RectangleShape shape{sf::Vector2f(50, 50)};. Hapax's solutions are compatible with pre-C++11 though.

jerryd, that's breaking all good encapsulation practice. You probably don't want to do that..