0

So recently I've been experimenting with C++ and SFML, as it seems perfect for a project I am aiming to do, however, I have been having difficulties even compiling the example code used to check my installation. the error list says that says 'event' uses undefined class 'sf::Event'. I have followed, very closely, the tutorial for the installation of SFML, tried both dynamically and statically linking SFML to my project, and directly including the .hpp file needed for this class, but to my dismay, I can still not fix this issue. I know it must be a fundamental issue with my installation, but I can not figure out what.

Here is the code anyway:

#include <C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\include\SFML\Graphics.hpp>


int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

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

        window.clear();
        window.draw(shape);
        window.display();
    }

Thanks!

EDIT:

Original error:

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'bool sf::Window::pollEvent(sf::Event &)': cannot convert argument 1 from 'int' to 'sf::Event &'    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   13  
Error (active)  E0070   incomplete type is not allowed  Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   12  
Error (active)  E0070   incomplete type is not allowed  Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   15  
Error (active)  E0070   incomplete type is not allowed  Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   15  
Error   C2079   'event' uses undefined class 'sf::Event'    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   12  
Error   C2027   use of undefined type 'sf::Event'   Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   15  
Error   C2065   'Closed': undeclared identifier Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.cpp   15  

Full error code with Event.hpp:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: virtual __thiscall sf::RenderWindow::~RenderWindow(void)" (??1RenderWindow@sf@@UAE@XZ) referenced in function _main Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall sf::VideoMode::VideoMode(unsigned int,unsigned int,unsigned int)" (??0VideoMode@sf@@QAE@III@Z) referenced in function _main  Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall sf::String::String(char const *,class std::locale const &)" (??0String@sf@@QAE@PBDABVlocale@std@@@Z) referenced in function _main    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: void __thiscall sf::Window::close(void)" (?close@Window@sf@@QAEXXZ) referenced in function _main    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: bool __thiscall sf::Window::isOpen(void)const " (?isOpen@Window@sf@@QBE_NXZ) referenced in function _main   Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: bool __thiscall sf::Window::pollEvent(class sf::Event &)" (?pollEvent@Window@sf@@QAE_NAAVEvent@2@@Z) referenced in function _main   Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: void __thiscall sf::Window::display(void)" (?display@Window@sf@@QAEXXZ) referenced in function _main    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall sf::Color::Color(unsigned char,unsigned char,unsigned char,unsigned char)" (??0Color@sf@@QAE@EEEE@Z) referenced in function _main    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: virtual __thiscall sf::Shape::~Shape(void)" (??1Shape@sf@@UAE@XZ) referenced in function "public: virtual __thiscall sf::CircleShape::~CircleShape(void)" (??1CircleShape@sf@@UAE@XZ)   Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: void __thiscall sf::Shape::setFillColor(class sf::Color const &)" (?setFillColor@Shape@sf@@QAEXABVColor@2@@Z) referenced in function _main  Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall sf::CircleShape::CircleShape(float,unsigned int)" (??0CircleShape@sf@@QAE@MI@Z) referenced in function _main Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: void __thiscall sf::RenderTarget::clear(class sf::Color const &)" (?clear@RenderTarget@sf@@QAEXABVColor@2@@Z) referenced in function _main  Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: void __thiscall sf::RenderTarget::draw(class sf::Drawable const &,class sf::RenderStates const &)" (?draw@RenderTarget@sf@@QAEXABVDrawable@2@ABVRenderStates@2@@Z) referenced in function _main Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2019 unresolved external symbol "public: __thiscall sf::RenderWindow::RenderWindow(class sf::VideoMode,class sf::String const &,unsigned int,struct sf::ContextSettings const &)" (??0RenderWindow@sf@@QAE@VVideoMode@1@ABVString@1@IABUContextSettings@1@@Z) referenced in function _main   Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2001 unresolved external symbol "public: static class sf::RenderStates const sf::RenderStates::Default" (?Default@RenderStates@sf@@2V12@B)   Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK2001 unresolved external symbol "public: static class sf::Color const sf::Color::Green" (?Green@Color@sf@@2V12@B)    Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Gravity1\Main.obj   1   
Error   LNK1120 16 unresolved externals Gravity1    C:\Users\xxxx_gpo4v3f\source\repos\Gravity1\Debug\Gravity1.exe  1   
Hyperil
  • 1
  • 2
  • It's going to be difficult to solve this one from the outside. Where is the class `Event `declared? It will need to be in the header file that you include, or in a file included by that [...]. It must be in the same `sf` namespace as the other classes, and it must not be excluded from compilation by any preprocessor symbol trickery (look for `#define` or `#ifdef` before `Event`) – Tim Randall Mar 30 '21 at 20:14
  • Include the full error message in your question. – super Mar 30 '21 at 20:32
  • 1
    Maybe if you add `#include `? Or even `#include ` just to be sure? – Galik Mar 30 '21 at 21:33
  • 1
    @Hyperil This should answer the new problem: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Galik Mar 31 '21 at 01:49

2 Answers2

-1

I suggest that you could add sfml by following the steps below.

  1. Properties->C/C++->Additional Include Directories: C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\include

  2. Properties->Linker->General->Additional Library Directories: C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\lib

  3. Properties -> Linker -> Input -> Additional Dependencies: sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-audio-d.lib

Then, you could refer to the code.

#include <SFML/Graphics.hpp>


int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

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

        window.clear();
        window.draw(shape);
        window.display();
    }

}

The result of running the code is as follows:

enter image description here

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7
-1

This is probably not the answer you're looking for but it's pretty difficult to resolve your issue without knowing where the mistake is being made during installation. That being said, (and assuming you're fine with a little spoon-feeding) if you're eager to just get it running, here's a template for Visual Studio 2019. Instructions are in the readme. Alternatively, try the following:

  • Check if you've correctly given the libraries and its dependencies (image attached).
  • Build from source.
  • Recheck all your directories.

Dependencies as stated on the official SFML site

Himan2104
  • 127
  • 5