使用类函数创建线程时出错
error while creating a thread with a class function
在下面的代码中,在thread t(&Fred::hello)
,我得到一个错误,该术语不评估为一个函数接受0个参数。问题是什么?
#include <iostream>
#include <thread>
using namespace std;
class Fred
{
public:
virtual void hello();
};
void Fred::hello()
{
cout << "hello" << endl;
}
int main()
{
thread t (&Fred::hello);
t.join();
return 0;
}
类T
的非静态成员函数需要在T
的实例上调用,并且需要一个隐式的第一个形参类型为T*
(或const,和/或volatile T*)。
Fred f;
f.hello()
等价于
Fred f;
Fred::hello(&f);
因此,当向线程构造函数传递非静态成员函数时,还必须传递隐含的第一个参数:
Fred f;
std::thread t(&Fred::hello, &f);
相关文章:
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- C++ 线程时出错,标准::调用:
- 混合开放MP/MPI代码中的多线程发送/接收时出错
- 将工作线程信号与主窗口插槽 (Qt5) 连接时出错
- std::c++ 中的线程在设置线程函数时不会出错
- std::move在将std::string移动到另一个线程时出错
- 编译类 C++ 中的线程时出错(错误 xthread)
- 在C++中创建线程时出错
- 在另一个线程正在运行的情况下退出应用程序时出错
- 在 Ubuntu 13.10 x64 C++11 中创建 std::线程时出错
- 提升时出错"member function redeclaration not allowed":线程
- 多个线程无法访问同一个指针而不会出错
- MFC 编程:编译时出错:线程代码中的错误
- 线程不刷新数据,在屏幕中显示数据时出错
- 线程调用类函数时出错
- JNI GetMethodID导致本机线程出错
- 读取线程应用程序(linux,pthreads)中的文件大小时出错
- 使用类函数创建线程时出错
- 在c++中创建简单线程时出错
- 在代码块中编译线程程序时出错