std :: thread< unresolved overloaded function type>错误

编程入门 行业动态 更新时间:2024-10-25 22:31:51
本文介绍了std :: thread< unresolved overloaded function type>错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从我的类中生成一个线程,线程在我的类中执行一个特定的方法。代码如下:

I am trying to spawn a thread from within my class and the thread executes a particular method in my class. The code looks like this:

class ThreadClass{ int myThread(int arg){ // do something } void createThread(){ thread t = thread(myThread,10); } } ;

这段代码在编译时会引发一个错误:

This code on compilation throws an error saying

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

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

I am not sure what is the actual bug here. Can someone help me with this?

感谢。

推荐答案

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

The problem is that a member function can't be called without an object. Provide a pointer to this so that the current object is used:

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

您可以使用任何 ThreadClass 对象,但在你的情况下,看起来这是正确的事情。

You could use an instance of any ThreadClass object, but in your case, it seems this is the right thing to do.

注意:对创建的线程的引用,以便稍后可以执行 join()。

NOTE: Remember you need a reference to the created thread so that you can do a join() later on.

更多推荐

std :: thread&lt; unresolved overloaded function type&gt;错误

本文发布于:2023-11-09 06:11:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   amp   lt   std   thread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!