2011-07-06 12 views
0

スレッドを使用するプログラムをコンパイルする際にエラーが発生します。ここに問題を引き起こしている部分があります。私が正しい方法でスレッド関数を呼び出しているかどうか誰にでも教えてくれればいいです。 main.cppにはmain()からスレッドメンバー関数を呼び出す方法

another_file.cppで
int main() 
{ 
    WishList w; 
    boost::thread thrd(&w.show_list); 
    thrd.join(); 
} 

class WishList{ 
public: 
     void show_list(); 
} 

void WishList::show_list(){ 
     . 
     . 
     . 
     . 
} 

私は次のエラーを取得していますが

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:用のBoostライブラリをインストール持つ問題スレッド。

+0

"main.cpp():"とはどういう意味ですか? "' main.cppのint main() '? – Johnsyweb

+0

はい。そうです –

答えて

5

メンバー関数のアドレスを取る構文は&ClassName::FunctionNameなので、&WishList::show_listにする必要がありますが、関数ポインタを呼び出すオブジェクトが必要になります。ベスト(かつ簡単な)がboost::bindを使用することです:スレッドを行うには

#include <boost/bind.hpp> 

WishList w; 
boost::thread t(boost::bind(&WishList::show_list, &w)); 
2

何が、これは単に「どのように私は、メンバ関数へのポインタを取得しない」ではありません。コンパイラが言うところの、&WishList::show_listと言ってください。しかし、インスタンスポインタを渡す必要があるかもしれません。

更新:はい、Xeoのようにbindとしてください。

タイトルについて:この関数は「スレッドに属していません」ということに注意してください。クラスはスレッドの一部ではありません。すべてのスレッドが同じメモリにアクセスします - 各スレッドには自動ストレージのための独自の領域がありますが、クラス定義には "これは別スレッドになります"ということはありません。

関連する問題