【问题标题】:std::thread <unresolved overloaded function type> errorstd::thread <未解决的重载函数类型> 错误
【发布时间】:2015-04-10 22:18:35
【问题描述】:

我正在尝试从我的类中生成一个线程,并且该线程在我的类中执行一个特定的方法。代码如下所示:

class ThreadClass{
    int myThread(int arg){
     // do something
    }

    void createThread(){
        thread t = thread(myThread,10);
    }

} ;

此代码编译时抛出错误提示

std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (ThreadClass::*)(int), _Args = {int}]
no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (ThreadClass::*&&)(int)’

我不确定这里的实际错误是什么。有人可以帮我解决这个问题吗?

谢谢。

【问题讨论】:

    标签: c++ multithreading c++11 std stdthread


    【解决方案1】:

    问题是没有对象就不能调用成员函数。提供指向this 的指针,以便使用当前对象:

    thread t(&ThreadClass::myThread, this, 10);
    

    您可以使用任何ThreadClass 对象的实例,但在您的情况下,似乎this 是正确的做法。

    注意:请记住,您需要对已创建线程的引用,以便稍后执行join()

    【讨论】:

    • 如果您稍后需要执行 join(),则需要对创建的线程的引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多