【问题标题】:sf::Event class is undefined, however SFML should've defined itsf::Event 类未定义,但 SFML 应该已定义它
【发布时间】:2021-03-30 20:06:17
【问题描述】:

所以最近我一直在尝试使用 C++ 和 SFML,因为它似乎非常适合我打算做的项目,但是,即使编译用于检查我的安装的示例代码,我也遇到了困难。错误列表显示'event' uses undefined class 'sf::Event'。 我非常密切地遵循安装 SFML 的教程,尝试将 SFML 动态和静态链接到我的项目,并直接包含此类所需的 .hpp 文件,但令我沮丧的是,我仍然无法解决这个问题.我知道这一定是我安装的一个根本问题,但我不知道是什么。

这里是代码:

#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();
    }

谢谢!

编辑:

原来的错误:

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  

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   

【问题讨论】:

  • 从外部解决这个问题会很困难。 Event declared 类在哪里?它需要在您包含的头文件中,或者在该 [...] 包含的文件中。它必须与其他类位于同一 sf 命名空间中,并且不得通过任何预处理器符号欺骗将其排除在编译之外(在 Event 之前查找 #define#ifdef
  • 在您的问题中包含完整的错误消息。
  • 如果您添加#include &lt;SFML/Window.hpp&gt; 可能吗?或者只是为了确定#include &lt;SFML/Window/Event.hpp&gt;
  • @Hyperil 这应该回答了新问题:stackoverflow.com/questions/12573816/…

标签: c++ visual-studio sfml


【解决方案1】:

我建议您可以按照以下步骤添加 sfml。

  1. 属性->C/C++->附加包含目录:C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\include

  2. 属性->链接器->常规->附加库目录:C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\lib

  3. 属性 -> 链接器 -> 输入 -> 附加依赖项:sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-audio-d.lib

然后,你可以参考代码。

#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();
    }

}

代码运行结果如下:

【讨论】:

  • 不幸的是这正是我所做的,但错误仍然存​​在
  • 你在Properties-&gt;All Configurations/All Platforms中设置了这些配置吗?
【解决方案2】:

这可能不是您要寻找的答案,但如果不知道安装过程中哪里出错,就很难解决您的问题。话虽这么说,(假设你可以用一点勺子喂食)如果你渴望让它运行,这里是template for Visual Studio 2019。说明在自述文件中。或者,尝试以下方法:

  • 检查您是否正确提供了库及其依赖项(附图片)。
  • 从源代码构建。
  • 重新检查所有目录。

【讨论】:

    猜你喜欢
    • 2017-03-11
    • 2018-08-27
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2018-07-19
    • 1970-01-01
    相关资源
    最近更新 更多