c++ - SFML 2.4.2-绘制文本时出现段错误

标签 c++ segmentation-fault sfml

运行此代码时出现段错误。它可能是错误的索引,但是我感到困惑,因为我尝试访问的元素存在,如“绘制”之前的“cout”所示。

我使用GCC:g++ -std = c++ 11 ./ellipse.cpp -o ./ellipse -Wfatal-errors -lsfml-graphics -lsfml-window -lsfml-system

我会继续搜索,但会有所帮助。

最好的祝福,

MC

#include <iostream>
#include "SFML/Graphics.hpp"
using namespace std;
struct ellipse
    {
    sf::Text ellipseTitle;
    };
vector<ellipse> initialize(vector<ellipse> ellipses,unsigned short numberOfEllipses)
    {
    sf::Font myFont;
    string myfontFileName="./media/Arial.ttf";
    if (!myFont.loadFromFile(myfontFileName))
        {
        cout << "Could not find the font " << myfontFileName << endl;
        }
    ellipse e;
    e.ellipseTitle.setFont(myFont);
    e.ellipseTitle.setCharacterSize(20);
    e.ellipseTitle.setFillColor(sf::Color::White);
    for(int i=0;i<numberOfEllipses;i++)
        {
        e.ellipseTitle.setString("[Some text "+to_string(i)+"]");
        sf::FloatRect textRect = e.ellipseTitle.getLocalBounds();
        e.ellipseTitle.setOrigin(textRect.left + textRect.width/2.0f,textRect.top  + textRect.height/2.0f);
        e.ellipseTitle.setPosition(sf::Vector2f(100,100+(20*i)));
        ellipses.push_back(e);
        }
    return ellipses;
    }
int main()
    {
    sf::RenderWindow window(sf::VideoMode(640,480), "Demo",sf::Style::Default);
    unsigned short numberOfEllipses=11;
    vector<ellipse> ellipses;
    ellipses=initialize(ellipses,numberOfEllipses);
    sf::Event myEvent;
    while (window.isOpen())
        {
        while (window.pollEvent(myEvent))
            {
            if (myEvent.type == sf::Event::EventType::Closed)
                {
                window.close();
                }
            }
        window.clear();
        for(ellipse e:ellipses)
            {
            cout << "title " << e.ellipseTitle.getString().toAnsiString() << endl;
            window.draw(e.ellipseTitle);
            }
        window.display();
        }
    return EXIT_SUCCESS;
    } ```

最佳答案

真的谢谢你经过更多搜索(知道如何命名问题),我发现这是一个非常常见的错误。这是更正的代码:

#include <stdio.h>
#include <iostream>
#include "SFML/Graphics.hpp"
using namespace std;
struct ellipse
    {
    sf::Text ellipseTitle;
    };
vector<ellipse> initialize(vector<ellipse> ellipses,unsigned short numberOfEllipses,sf::Font& myFont)
    {
    ellipse e;
    e.ellipseTitle.setFont(myFont);
    e.ellipseTitle.setCharacterSize(20);
    e.ellipseTitle.setFillColor(sf::Color::White);
    for(int i=0;i<numberOfEllipses;i++)
        {
        e.ellipseTitle.setString("[Some text "+to_string(i)+"]");
        sf::FloatRect textRect = e.ellipseTitle.getLocalBounds();
        e.ellipseTitle.setOrigin(textRect.left + textRect.width/2.0f,textRect.top  + textRect.height/2.0f);
        e.ellipseTitle.setPosition(sf::Vector2f(100,100+(20*i)));
        ellipses.push_back(e);
        }
    return ellipses;
    }
int main()
    {
    sf::RenderWindow window(sf::VideoMode(640,480), "Demo",sf::Style::Default);
    unsigned short numberOfEllipses=11;
    sf::Font myFont;
    string myfontFileName="./media/Arial.ttf";
    if (!myFont.loadFromFile(myfontFileName))
        {
        cout << "Could not find the font " << myfontFileName << endl;
        return EXIT_FAILURE;
        }
    vector<ellipse> ellipses;
    ellipses=initialize(ellipses,numberOfEllipses,myFont);
    sf::Event myEvent;
    while (window.isOpen())
        {
        while (window.pollEvent(myEvent))
            {
            if (myEvent.type == sf::Event::EventType::Closed)
                {
                window.close();
                }
            }
        window.clear();
        for(ellipse e:ellipses)
            {
            cout << "title " << e.ellipseTitle.getString().toAnsiString() << endl;
            window.draw(e.ellipseTitle);
            }
        window.display();
        }
    return EXIT_SUCCESS;
    }```

关于c++ - SFML 2.4.2-绘制文本时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59459450/

相关文章:

c++ - 实现 pimpl 习惯用法时出现链接器错误

c++ - 尝试将两个视频与 pHash 库及其 ruby​​ 绑定(bind)进行比较时出现段错误

c++ - 如何阻止着色器扭曲纹理

c++ - "The C++ Library doesen' t 为这种类型提供散列。”- 在 std::unordered_map 中使用自己的类

c++ - GTX 550 Ti显卡支持的动态并行编程?

c++ - 为什么某些类型的字符串连接明显快于其他类型?

c - 在 yacc 中返回指针时出现段错误

c++ - Visual Studio SFML 链接器错误

c++ - 当我需要一个字符串时,我可以用 unsigned char* 做什么?

c++ - 调用派生类的虚函数时出现段错误