简体   繁体   中英

boost::thread invalid use of a non-static member function

I'm begining with the boost::thread library and I have a code like this :

Class::Class()
{
    ...
    boost::thread thread_foo(Class::foo);
    ...
}

Class::foo()
{
    //do stuff
}

But when I compile it, I have an "invalid use of a non-static member function" and I don't really know what is wrong because when I look at the doc this is the way to create a thread.

I'm sure it's a silly mistake but I just don't see it.

Thanks

You should send object also.

boost::thread thread_foo(&Class::foo, this);

or

boost::thread thread_foo(boost::bind(&Class::foo, this));

All non-static member function have a hidden first argument, that is the this pointer in the member function.

If you don't provide the instance to call the member function on, then this will be undefined and you will have undefined behavior .

Statis member functions, on the other hand, does not have this hidden argument, and so can be called without an instance.

To solve your problem, either listen to the compiler message and make your member function static. Or you pass an argument to the thread function, and that argument must be the instance to call the function on (normally this ).

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