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.

you are viewing a single comment's thread
view the rest of the comments
[–] 1 point 3 days ago

I think it's complaining that you don't have the include statement for std::optional. Try adding #include <optional> above the SFML include.

  • source