SFML:没有与Thread()匹配的函数

SFML: no matching function to Thread()

本文关键字:函数 Thread SFML      更新时间:2023-10-16

我尝试在我的游戏中使用SFML线程,但我遇到了一个问题。我的代码:

void MyGame::endGame()
{
    sf::Thread thread(&PuzzleGame::endThread);
    thread.Launch();
}
void MyGame::endThread()
{
}

结果是:

../src/MyGame.cpp: In member function ‘void MyGame::endGame()’:
../src/MyGame.cpp:186:51: error: no matching function for call to ‘sf::Thread::Thread(void (MyGame::*)())’
sf::Thread thread(&MyGame::endThread);
                                    ^

什么是问题?

编辑我使用SFML 1.6

thread.launch();

注意小写

如果你感兴趣,这里是文档:

http://www.sfml-dev.org/tutorials/2.0/system-thread.php

如果你想传递一个类函数,你还需要传递你想调用它的对象:

void MyGame::endGame()
{
    sf::Thread thread(&MyGame::endThread, this);
    thread.launch();
}
void MyGame::endThread()
{
}

或者您可以使用静态类方法:

void MyGame::endGame()
{
    sf::Thread thread(&MyGame::endThread);
    thread.launch();
}
static void MyGame::endThread()
{
}

请阅读SFML线程的完整文档。创建线程的局部变量是没有帮助的。当超出作用域并且您希望线程运行时,它将被销毁,而不是被删除。

我看到你正在使用旧的SFML 1.6。请仔细阅读教程。在旧版本中,您只能使用上述两个选项中的第二个选项。您可能希望尽快切换到2.0或2.1。

相关文章: