我在编译使用线程的程序时遇到错误.这是导致问题的部分.如果有人告诉我是否以正确的方式调用线程函数,那将会很好.
在main.cpp中:
int main()
{
WishList w;
boost::thread thrd(&w.show_list);
thrd.join();
}
Run Code Online (Sandbox Code Playgroud)
在another_file.cpp中:
class WishList{
public:
void show_list();
}
void WishList::show_list(){
.
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
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) (...)’
Run Code Online (Sandbox Code Playgroud)
编辑:在为线程安装Boost库时遇到问题.一旦它工作,请尽快尝试
获取成员函数地址的语法&ClassName::FunctionName应该是&WishList::show_list,但现在需要一个对象来调用函数指针.最好(也是最简单)是使用boost::bind:
#include <boost/bind.hpp>
WishList w;
boost::thread t(boost::bind(&WishList::show_list, &w));
Run Code Online (Sandbox Code Playgroud)