abd*_*eem 2 c++ multithreading visual-c++ c++11
下面是我正在运行的代码,它在我在线程对象 t1 和 t2 中传递重载函数“myfunc”的行中引发错误(也用注释标识)
#include<iostream>
#include<thread>
using namespace std;
void myfunc(int x)
{
cout << x << endl;
}
void myfunc(int x,int y)
{
cout << x << " " << y << endl;
}
int main()
{
thread t1(myfunc,1);//error here
thread t2(myfunc, 1,2);//error here
t1.join();
t2.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误声明:
错误 1: 错误(活动)E0289 没有构造函数的实例“std::thread::thread”匹配参数列表参数类型是:(unknown-type,int)
错误 2: 错误(活动)E0289 没有构造函数“std::thread::thread”的实例匹配参数列表参数类型是:(unknown-type,int,int)
当您有作为参数传递的重载函数时,您需要帮助编译器。
可能的解决方案:
using f1 = void(*)(int);
using f2 = void(*)(int, int);
thread t1(static_cast<f1>(myfunc), 1);
thread t2(static_cast<f2>(myfunc), 1, 2);
Run Code Online (Sandbox Code Playgroud)