简体   繁体   中英

std::thread <unresolved overloaded function type> error

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?

Thanks.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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