Search code examples
c++sfml

Can't implement pure virtual function from SFML


SFML defines this virtual function:

class SFML_GRAPHICS_API Drawable
{
public: virtual ~Drawable() {}

protected:
    friend class RenderTarget;
    virtual void draw(RenderTarget& target, RenderStates states) const = 0;
};

And in my code on surface.hpp I created the following class:

class surface : public sf::Drawable
{
    friend class vertex;
public:
    surface(sf::Vector2f p1, sf::Vector2f p2, sf::Vector2f p3, sf::Vector2f p4);
    ~surface();
    vertex& operator[](int i);
private:
    void draw(sf::RenderTarget& target, sf::RenderStates states);
};

And in surface.cpp I provide the implementation for draw:

void surface::draw(sf::RenderTarget& target, sf::RenderStates states){
    // ...
}

But when I run my code and try to surface mySurface(/* ... */); from my main.cpp, I get the error:

SFML/Graphics/Drawable.hpp:69:18: Unimplemented pure virtual method 'draw' in 'surface'

I'm almost 100% sure I implemented it right, so why isn't clang letting me compile? I also tried to define draw inside the definition of surface in surface.hpp but nothing helps...


Solution

  • I'm almost 100% sure I implemented it right

    Good thing you said "almost".

    virtual void draw(RenderTarget& target, RenderStates states) const = 0;
                                                                 ^^^^^
    

    The const is part of the function signature! It must appear in your implementation too:

    class surface : public sf::Drawable 
    {
        // ...
        void draw(sf::RenderTarget& target, sf::RenderStates states) const;
                                                                     ^^^^^
    };