不能将类成员函数传递给另一个函数(std::thread::thread)
Cannot pass class member-function to another function(std::thread::thread)
看看这两个代码。
下面的代码工作正常。
void someFunction () {
// Some unimportant stuff
}
MainM::MainM(QObject *parent) :
QObject(parent)
{
std::thread oUpdate (someFunction);
}
此代码引发错误:
void MainM::someFunction () { //as a class member
}
MainM::MainM(QObject *parent) :
QObject(parent)
{
std::thread oUpdate (someFunction);
}
错误:
error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
std::thread oUpdate (someFunction);
^
不能
通过将&
应用于名称来创建指向成员函数的指针。您需要完全合格的成员:&MainM::someFunction
。
并且还通过传递this
将其绑定到实例,例如
#include <thread>
struct MainM
{
void someFunction() {
}
void main()
{
std::thread th(&MainM::someFunction, this);
}
};
int main()
{
MainM m;
m.main();
}
相关文章:
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- 将模板(没有规范)传递给 std::thread() 会出现错误:<未解析的重载函数类型>匹配错误
- 在没有复制构造函数的对象的成员函数中启动 std::thread
- 无法将参数传递给 boost::thread 构造函数
- CLang:在 std::thread 中运行函数会导致结构创建BAD_ACCESS
- 如何在 UWP C++/CX 中对象的成员函数中使用 std::thread
- 使用 std::thread & std::bind 在成员函数中启动线程
- 错误:调用 std::thread 没有匹配函数
- 有什么理由C++ 11+ std::mutex 应该声明为全局变量,而不是作为函数参数传递到 std::thread 中
- std::thread从函数启动,无需等待线程完成
- 使用 boost::thread 运行函子,但析构函数被错误地调用
- 睡眠影响 std::thread 调用哪个虚拟成员函数?
- 如何在不接触函数本身的情况下暂停和恢复 C++11 std::thread 的功能
- C++ - 在 std::thread 中调用重载函数时编译失败
- 子类化 std::thread:构造器中可变参数模板函数的问题
- 如果容器不是调用函数中的引用,则使用 std::thread 传递对迭代器的引用将失败
- 标准库中是否有与 std::thread 的构造函数语义匹配的类型擦除函数包装器?
- 将回调函数中对对象的引用传递给 std::thread
- thread.detach() 函数的左值何时会超出以下代码的范围
- VSCod 中的C++:错误:没有用于初始化'std::thread'的匹配构造函数