【发布时间】:2018-05-07 07:49:58
【问题描述】:
我有一个名为levelButtons的std::vector<Button>,它像这样存储Buttons:
for (int i = 1; i <= 8; i++) {
for (int j = 0; j < 4; j++) {
Button level(50, 50, std::bind(&Game::continueGame, this, (j * 8) + i), " " + std::to_string((j * 8) + i), &levelTexture);
level.setPosition(50 + (i * 100), 200 + (j * 100));
levelButtons.push_back(level);
}
}
然后我像这样画它们:
for (int i=0; i<levelButtons.size(); i++) {
win->draw(levelButtons[i]);
}
其中win 是RenderWindow*。 Button 类如下所示:
Button::Button(float width, float height, std::function<void()> onclick, String face, Color fillColour) {
arial.loadFromFile("/Users/mmysteriouss/Downloads/arial.ttf");
pointerTexture.loadFromFile("/Users/mmysteriouss/Downloads/pointer.png");
pointer.setTexture(pointerTexture);
pointer.scale(0.04, 0.04);
buttonface.setFont(arial);
buttonface.setString(face);
buttonface.setCharacterSize(30);
buttonface.setFillColor(Color::Black);
outline.setSize(Vector2f(width, height));
outline.setOutlineColor(Color::Black);
outline.setFillColor(fillColour);
click = onclick;
}
Button::Button(float width, float height, std::function<void()> onclick, String face, Texture *fillTexture) {
arial.loadFromFile("/Users/mmysteriouss/Downloads/arial.ttf");
pointerTexture.loadFromFile("/Users/mmysteriouss/Downloads/pointer.png");
pointer.setTexture(pointerTexture);
pointer.scale(0.04, 0.04);
buttonface.setFont(arial);
buttonface.setString(face);
buttonface.setCharacterSize(30);
buttonface.setFillColor(Color::White);
outline.setSize(Vector2f(width, height));
outline.setOutlineColor(Color::Black);
outline.setTexture(fillTexture);
click = onclick;
}
void Button::handleClick(float x, float y) {
if (outline.getGlobalBounds().contains(x, y)) {
click();
}
}
void Button::setPosition(float x, float y) {
outline.setPosition(x, y);
buttonface.setPosition(x, y);
}
void Button::handleMove(float x, float y) {
hovering = outline.getGlobalBounds().contains(x, y);
pointer.setPosition(x - 15, y - 5);
}
它会抛出一个
分段错误:11;
报告如下所示:
thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsfml-graphics.2.4.dylib 0x0000000107e0c6f7 sf::Font::setCurrentSize(unsigned int) const + 23
1 libsfml-graphics.2.4.dylib 0x0000000107e0c914 sf::Font::getUnderlinePosition(unsigned int) const + 30
2 libsfml-graphics.2.4.dylib 0x0000000107e35c30 sf::Text::ensureGeometryUpdate() const + 202
3 libsfml-graphics.2.4.dylib 0x0000000107e363aa sf::Text::draw(sf::RenderTarget&, sf::RenderStates) const + 38
4 libsfml-graphics.2.4.dylib 0x0000000107e29dfd sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) + 41
5 光 0x0000000107dcd081 Button::draw(sf::RenderTarget&, sf::RenderStates) const + 81 (Button.h:43)
6 libsfml-graphics.2.4.dylib 0x0000000107e29dfd sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) + 41
7 光 0x0000000107dd43c2 游戏::draw() + 210 (Game.cpp:26)
8 光 0x0000000107dd4bc5 AbsGame::render() + 69 (AbsGame.h:44)
9 光 0x0000000107dd4910 main + 496 (main.cpp:27)
10 libdyld.dylib 0x00007fff76f58015 开始 + 1
为什么会引发分段错误?我很确定我的迭代很好,而且我的 Button 类可以单独工作 - 我创建了一个主文件来测试它并且它工作......
【问题讨论】:
-
如果您使用的是 linux,请尝试使用 valgrind 获取有关 segfault 的更多信息
-
您使用哪种操作系统和 IDE?
-
@Tyker 我在 mac + 我用过那个报告
-
@MauriceRandomNumber Mac OS X High Sierra Netbeans IDE 8.2
-
您的 Button 类复制/移动构造函数是什么样的?在您的循环中,您在堆栈上创建按钮,然后将其移动或复制到向量中,临时对象被删除。如果你没有正确的复制/移动构造函数,就会出现问题。
标签: c++ segmentation-fault drawable sfml