Issue
This Content is from Stack Overflow. Question asked by Marco Becerra
I am learning to use the SFML library, I am following a course on youtube and I can’t get it to compile this part of the program.
class Character: public sf::Drawable{
public:
sf::Sprite _sprite;
sf::Texture _texture;
float _velocity = 6;
void draw(sf::RenderTarget& target, const sf::RenderStates states) override{
target.draw(_sprite, states);
}
Instantiating the object in main.cpp
#include <SFML/Graphics.hpp>
#include "character.h"
int main()
{
sf::RenderWindow window(sf::VideoMode({1343, 715}), "SFML");
window.setFramerateLimit(60);
Character sasuke;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
sasuke.update();
window.clear();
window.draw(sasuke);
window.display();
}
return 0;
}
Error and a note:
main.cpp: In function ‘int main()’:
main.cpp:9:15: error: cannot declare variable ‘sasuke’ to be of abstract type ‘Character’
9 | Character sasuke;
| ^~~~~~
character.h:3:7: note: because the following virtual functions are pure within ‘Character’:
3 | class Character: public sf::Drawable{
Solution
Your const
is misplaced
void draw(sf::RenderTarget& target, sf::RenderStates states) const override{
It’s the draw
method that should be declared const
, not the states
parameter.
This Question was asked in StackOverflow by Marco Becerra and Answered by john It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.