我正在尝试为我的 OpenGL 项目创建一个加载屏幕,并已阅读该内容以使其正常工作,最好使用线程。我试图用我的线程调用我的函数,但我不断收到这些错误:
error C2064: term does not evaluate to a function taking 3 arguments
IntelliSense: no instance of constructor "std::thread::thread" matches the argument list argument types are: (void (Screen* newScreen, bool activeVisuals, bool activeControls), PlayScreen *, bool, bool)
这是我的代码:
//LoadingScreen
class LoadingScreen
{
LoadingScreen();
void LoadNewScreen(Screen* newScreen, bool activeVisuals, bool activeControls);
void Setup();
};
void LoadingScreen::LoadNewScreen(Screen* newScreen, bool activeVisuals, bool activeControls)
{
}
void LoadingScreen::Setup()
{
PlayScreen *playScreen = new PlayScreen();
std::thread first(LoadingScreen::LoadNewScreen,playScreen, true, true);// , playScreen, true, true);
first.join();
}
//source.cpp
LoadingScreen loadingScreen;
int main()
{
LoadingScreen loadingScreen = LoadingScreen();
loadingScreen.Setup();
return 0;
}
最佳答案
您需要传递一个附加参数,该参数是其成员函数作为第一个参数传递的类的实例。
std::thread first(&LoadingScreen::LoadNewScreen, this, playScreen, true, true);
// ^^^^ <= instance of LoadingScreen
需要额外的参数,因为这是实际调用 LoadNewScreen
的参数。
this->LoadNewScreen(playScreen, true, true);
关于c++ - 如何在类里面使用线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28574004/