繁体   English   中英

的std ::线程 <unresolved overloaded function type> 错误

[英]std::thread <unresolved overloaded function type> error

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

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)’

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

谢谢。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM