When I am compiling the following piece of code, I am getting the following error. Can anyone help me in resolving this issue. Thank you.
error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&foo::abc’ [-fpermissive]
boost::thread testThread(boost::bind(&f.abc, f));
........................................................................^
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
class foo
{
private:
public:
foo(){}
void abc()
{
std::cout << "abc" << std::endl;
}
};
int main()
{
foo f;
boost::thread testThread(&f.abc, f);
return 0;
}
The error message couldn't really be any clearer
Say ‘&foo::abc’
boost::thread testThread(boost::bind(&foo::abc, f));
// ^^^^^^^
Also, there's no need for boost::bind
, this should work too
boost::thread testThread(&foo::abc, f);
Be aware that both of these make copies of f
, if you want to avoid that you should use either of the following
testThread(&foo::abc, &f);
testThread(&foo::abc, boost::ref(f));
Now, why on earth is main()
a member function of class zoo
??
Do as the error says, replace f.abc
with foo::abc
:
boost::thread testThread(boost::bind(&foo::abc, f));
Use:
boost::thread testThread(boost::bind(&foo::abc, f));
Do as @Praetorian said. Everything works fine:
//Title of this code
//Compiler Version 18.00.21005.1 for x86
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
class zoo;
class foo
{
private:
public:
foo(){}
void abc()
{
std::cout << "abc" << std::endl;
}
};
class zoo
{
public:
int main()
{
foo f;
boost::thread testThread(boost::bind(&foo::abc, &f));
testThread.join();
return 0;
}
};
int main()
{
zoo{}.main();
}
Live: http://rextester.com/XDE37483