I followed the directions on SFML’s website for installation and usage via CMake. Cloned the git repo and ran cmake -B build cmake --build build. Everything went fine, but when I try to compile the test program with g++, it fails with a few different errors.
Here’s the errors:
src/main.cpp: In function ‘int main()’:
src/main.cpp:11:45: error: qualified-id in declaration before ‘event’
11 | while ( const std::optional event = window.pollEvent() )
| ^~~~~
src/main.cpp:13:30: error: ‘event’ was not declared in this scope
13 | if ( event->is<sf::Event::Closed>() )
| ^~~~~
src/main.cpp:13:59: error: expected primary-expression before ‘)’ token
13 | if ( event->is<sf::Event::Closed>() )
| ^
And the main file:
#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() )
{
while ( const std::optional event = window.pollEvent() )
{
if ( event->is<sf::Event::Closed>() )
window.close();
}
window.clear();
window.draw( shape );
window.display();
}
}
It should work to my knowledge, I think. I mean, this is straight from SFML’s github and tutorial page on their website, so it should be functional as-is once the cmake stuff is handled, right?
EDIT: Figured it out. Had to build the executable from the build folder via make.
while ( const std::optional event = window.pollEvent() )Try passing
-std=c++23to g++ – that code doesn’t look like valid C++ from when I was regularly programming in C++, but the standards committee has fucked around with the language a lot in the last few years and I suspect this is something from one of the very recent standards.Hey, thanks, I tried it but it didn’t work. Ended up figuring out I had to build the project from the build folder with
make .which i wasn’t aware of.
Which C++ standard are you using? I think you should look into explicitly specifying at least C++11 as the error is whithin the if(declare var)… world.
Afaik this is not supported before C++11.
I think it’s complaining that you don’t have the include statement for std::optional. Try adding
above the SFML include.


