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

Author Topic: Overriding sf::Drawable::draw  (Read 2863 times)

0 Members and 1 Guest are viewing this topic.

dacox

  • Newbie
  • *
  • Posts: 2
    • View Profile
Overriding sf::Drawable::draw
« on: May 16, 2021, 01:53:41 pm »
I'm very new to SFML, so please be gentle,

I have a Particle class that I want to make drawable, so I want it to inherit sf::Drawable and override the draw function so that I can use window.draw(myClass). However, when I try to instantiate a particle from the class, Visual Studio tells me that "pure virtual function "sf::Drawable::draw" has no overrider" even though I've followed everything the docs said. I've dug through some similar questions on this forum already, as well as a couple of stackoverflow questions, and nothing has helped - either I've already done whatever the response suggests doing or the original asker didn't have the same problem as me.
Here's the relevant part of Particle.cpp:
void Particle::draw(sf::RenderWindow& window, sf::RenderStates states) const {
        window.draw(body);
}
 

Here's Particle.h:
class Particle : public sf::Drawable {
public:
        void update();
        void show();
        Particle();
        float getMass();
       

private:
        float x;
        float y;
        float radius;
        sf::CircleShape body;
        void draw(sf::RenderWindow& window, sf::RenderStates states) const;
};

and here's the relevant part of main.cpp:
        sf::RenderWindow window(sf::VideoMode(800, 800), "qinetiq");

        Particle particle1 = Particle();

        while (window.isOpen()) {
                sf::Event event;

                while (window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed) {
                                window.close();
                        }
                }
                window.clear(sf::Color::Black);

                window.draw(particle1);

Any and all help is massively appreciated, this is my first post on this forum, so if I'm doing something wrong please let me know.

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Overriding sf::Drawable::draw
« Reply #1 on: May 16, 2021, 02:08:06 pm »
To override the draw function in an inherited class, you need to exactly match the function's signature.
The Drawable class has:
draw(RenderTarget& target, RenderStates states)
But yours is
draw(sf::RenderWindow& window, sf::RenderStates states)
(The sf namespace bit is fine)
A RenderTarget can refer to a RenderWindow or a RenderTexture.

dacox

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Overriding sf::Drawable::draw
« Reply #2 on: May 16, 2021, 02:44:56 pm »
@kojack
Absolute legend, thank you so much, can't believe I missed that.