0

The following piece of code is giving out an error message, and I am not too sure what is wrong with the piece of code:

#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

void draw (sf::RenderWindow* window, vector<sf::Drawable> drawableObjects) {
    for (unsigned short i = 0; i < drawableObjects.size(); i++) {
        window->draw(drawableObjects[i]);
    }
    window->display();
}


int main(int argc, char *argv[]) {
    sf::RenderWindow window (sf::VideoMode(512, 512), "Platformer", sf::Style::Close | sf::Style::Titlebar);
    sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));

    while (window.isOpen()) {
        sf::Event evnt;
        while (window.pollEvent(evnt)) {
            if (evnt.type == evnt.Closed) {
                window.close();
            }
        }
    }
    draw(&window, {player});


    return 0;
}

I compiled the code with the command:

    g++ *.cpp -o ../build/a.out -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network -o ../build/a.out

The error message that g++ returns is:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:26:27: error: cannot allocate an object of abstract type ‘sf::Drawable’
   26 |     draw(&window, {player});
      |                           ^
In file included from /usr/include/SFML/Graphics/Shape.hpp:32,
                 from /usr/include/SFML/Graphics/CircleShape.hpp:32,
                 from /usr/include/SFML/Graphics.hpp:34,
                 from main.cpp:2:
/usr/include/SFML/Graphics/Drawable.hpp:44:25: note:   because the following virtual functions are pure within ‘sf::Drawabl
’:                                                                                                                         
   44 | class SFML_GRAPHICS_API Drawable
      |                         ^~~~~~~~
/usr/include/SFML/Graphics/Drawable.hpp:69:18: note:     ‘virtual void sf::Drawable::draw(sf::RenderTarget&, sf::RenderStates) const’
   69 |     virtual void draw(RenderTarget& target, RenderStates states) const = 0;
      |                  ^~~~
make: *** [Makefile:8: buildAndRun] Error 1

Any help will be much appreciated, Thanks

Aadi J
  • 1
  • 1
    it tells you the problem, the class `Drawable` is ABC that mean that you cant make instance of that class because the function `draw` is pure virtual read [this](https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/) for more information – yaodav Apr 08 '21 at 08:12
  • Oh that makes a lot more sense, thanks a lot. – Aadi J Apr 08 '21 at 08:32
  • also, try not to use [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – yaodav Apr 08 '21 at 08:51

0 Answers0