I'm getting errors while compiling a program that uses threads. Here is the part that is causing problems.It would be nice if anybody told me if I'm calling the thread function in the right way .
In main.cpp:
int main()
{
WishList w;
boost::thread thrd(&w.show_list);
thrd.join();
}
In another_file.cpp:
class WishList{
public:
void show_list();
}
void WishList::show_list(){
.
.
.
.
}
I'm getting the following error
error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&WishList::show_list’
/home/sharatds/Downloads/boost_1_46_1/boost/thread/detail/thread.hpp: In member function ‘void boost::detail::thread_data<F>::run() [with F = void (WishList::*)()]’:
/home/sharatds/Downloads/boost_1_46_1/boost/thread/detail/thread.hpp:61:17: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((boost::detail::thread_data<void (WishList::*)()>*)this)->boost::detail::thread_data<void (WishList::*)()>::f (...)’, e.g. ‘(... ->* ((boost::detail::thread_data<void (WishList::*)()>*)this)->boost::detail::thread_data<void (WishList::*)()>::f) (...)’
EDIT : Having problems installing Boost library for threads. Shall try this as soon as it works
The syntax to take the address of a member function is &ClassName::FunctionName
, so it should be &WishList::show_list
, but now you need an object to call the function pointer on. Best (and easiest) is to use boost::bind
:
#include <boost/bind.hpp>
WishList w;
boost::thread t(boost::bind(&WishList::show_list, &w));