Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: trouble running sample code from tutorial  (Read 969 times)

0 Members and 1 Guest are viewing this topic.

dwfunk4475

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
trouble running sample code from tutorial
« on: May 03, 2025, 08:41:45 am »
Windows 11, MingW64, built SFML.

On the tutorial "Opening and managing an SFML window". Running the sample code, I get the error "event was not declared in this scope."
What have I overlooked?



Code: [Select]

#include <SFML/Window.hpp>


int main()
{
    sf::Window window(sf::VideoMode({800, 600}), "My window");


    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        while (const std::optional event = window.pollEvent())
        {
            // "close requested" event: we close the window
            if (event->is<sf::Event::Closed>())
                window.close();
        }
    }
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11193
    • View Profile
    • development blog
    • Email
Re: trouble running sample code from tutorial
« Reply #1 on: May 03, 2025, 12:37:36 pm »
Make sure you're really compiling the code you've copied here. For example check that only the wanted *.cpp file is being compiled and not something else.
Make sure you're compiling with C++17 enabled.

What's the complete error you're getting?

The code should beyond the missing header include be correct.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dwfunk4475

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: trouble running sample code from tutorial
« Reply #2 on: May 04, 2025, 06:06:30 pm »
C++ is set. IDE is VS Code

terminal output:

Code: [Select]
Executing task: C/C++: g++.exe build active file

Starting build...
cmd /c chcp 65001>nul && C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\VSCode_Projects\Timber\myWindow.cpp -o C:\VSCode_Projects\Timber\myWindow.exe
C:\VSCode_Projects\Timber\myWindow.cpp: In function 'int main()':
C:\VSCode_Projects\Timber\myWindow.cpp:11:36: error: qualified-id in declaration before 'event'
   11 |         while (const std::optional event = window.pollEvent())
      |                                    ^~~~~
C:\VSCode_Projects\Timber\myWindow.cpp:14:17: error: 'event' was not declared in this scope
   14 |             if (event->is<sf::Event::Closed>())
      |                 ^~~~~
C:\VSCode_Projects\Timber\myWindow.cpp:14:46: error: expected primary-expression before ')' token
   14 |             if (event->is<sf::Event::Closed>())
      |                                              ^

Build finished with error(s).

 *  The terminal process terminated with exit code: -1.
 *  Terminal will be reused by tasks, press any key to close it.



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11193
    • View Profile
    • development blog
    • Email
Re: trouble running sample code from tutorial
« Reply #3 on: May 04, 2025, 09:31:21 pm »
I don't see -std=c++17 on the build command.

For VS Code (and other IDEs) we recommend the CMake template: https://github.com/SFML/cmake-sfml-project
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dwfunk4475

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: trouble running sample code from tutorial
« Reply #4 on: May 09, 2025, 03:02:13 am »
OK, thanks for the feedback.
C++17 is set, or so I thought, I'll have to figure out why it's not working.

Code: [Select]
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:/SFML/include",
                "C:\\SFML\\include\\SFML\\Window",
                "C:\\SFML\\include\\SFML\\Graphics"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22621.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11193
    • View Profile
    • development blog
    • Email
Re: trouble running sample code from tutorial
« Reply #5 on: May 09, 2025, 07:10:39 am »
If you want to use MSVC as compiler, you need to set up your tasks.json correctly (or use the CMake extension ;) ).
See also: https://code.visualstudio.com/docs/cpp/config-msvc
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/