使用SFML RenderWindow时,内存位置的std::length_error

std::length_error at memory location When using SFML RenderWindow

本文关键字:std length error 位置 RenderWindow SFML 内存 使用      更新时间:2023-10-16

我的项目使用SFML,目前正在学习网站上的教程。

目前,我只完成了使用以下代码(直接从教程中粘贴)创建RenderWindow的第一步:

#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
int main()
{
    // create the window
    sf::VideoMode vm (800, 600);
    sf::RenderWindow window(vm, "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
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
        // clear the window with black color
        window.clear(sf::Color::Black);
        // draw everything here...
        // window.draw(...);
        // end the current frame
        window.display();
    }
    return 0;
}

它编译得很好,但当我运行它时,我会得到以下异常:

First-chance exception at 0x76BFC42D in OpenGlTest.exe: Microsoft C++ 
exception: std::length_error at memory location 0x0046F754.    

我尝试调试并将std::length_error跟踪到RenderWindow构造函数:

sf::RenderWindow window(vm, "My window");      

我对c++和OpenGL非常缺乏经验,所以我不知道如何继续,它不会让我进入构造函数查看发生了什么,它只是立即抛出异常。

感谢您的帮助。

我想我发现了问题。我不确定这是修复还是运气好,但如果在使用Visual C++时链接到sfml-xxxx.lib而不是sfml-xxxx-d.lib,显然会发生崩溃。在那次改变之后,它似乎确实起了作用。

我不是SFML的专家,但当你创建这样的窗口时,会有窗口按钮,它们总共需要10多个像素。

你试过加大码吗?试试200,就像教程中所做的那样。