Reputation: 8505
In below code, at thread t(&Fred::hello)
i am getting an error that the term does not evaluate to a function taking 0 arguments. What is the issue?
#include <iostream>
#include <thread>
using namespace std;
class Fred
{
public:
virtual void hello();
};
void Fred::hello()
{
cout << "hello" << endl;
}
int main()
{
thread t (&Fred::hello);
t.join();
return 0;
}
Upvotes: 0
Views: 78
Reputation: 227390
A non-static member function of class T
needs to be called on an instance of T
, and takes an implicit first parameter of type T*
(or const, and/or volatile T*).
So
Fred f;
f.hello()
is equivalent to
Fred f;
Fred::hello(&f);
So when you pass a non-static member function to a thread constructor, you must pass the implicit first argument too:
Fred f;
std::thread t(&Fred::hello, &f);
Upvotes: 4