最近我一直在尝试 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
这可能不是您正在寻找的答案,但如果不知道安装过程中哪里出了错误,就很难解决您的问题。话虽这么说,如果您渴望让它运行,这里有一个 Visual Studio 2019 模板。说明位于自述文件中。或者,尝试以下操作:
您可以按照以下步骤添加 sfml。
属性->C/C++->其他包含目录:
C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\include
属性->链接器->常规->其他库目录:
C:\Users\xxx_gpo4v3f\Documents\SFML-2.5.1-windows-vc15-64-bit\SFML-2.5.1\lib
属性 -> 链接器 -> 输入 -> 其他依赖项:
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();
}
}
代码运行结果如下: