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

Author Topic: Shape Functions not working in classes.  (Read 3778 times)

0 Members and 1 Guest are viewing this topic.

Bonediggerninja

  • Newbie
  • *
  • Posts: 10
    • View Profile
Shape Functions not working in classes.
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Shape Functions not working in classes.
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jerryd

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Shape Functions not working in classes.
« Reply #2 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));
       
}
 


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Shape Functions not working in classes.
« Reply #3 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)
        {
        }
};
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Shape Functions not working in classes.
« Reply #4 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..
SFML / OS X developer