私は C++ 11 スレッドを使い始めたばかりで、(おそらくばかげた) エラーで苦労しています。これは私のサンプルプログラムです:
#include <iostream>
#include <thread>
#include <future>
using namespace std;
class A {
public:
A() {
cout << "A constructor\n";
}
void foo() {
cout << "I'm foo() and I greet you.\n";
}
static void foo2() {
cout << "I'm foo2() and I am static!\n";
}
void operator()() {
cout << "I'm the operator(). Hi there!\n";
}
};
void hello1() {
cout << "Hello from outside class A\n";
}
int main() {
A obj;
thread t1(hello1); // it works
thread t2(A::foo2); // it works
thread t3(obj.foo); // error
thread t4(obj); // it works
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
}
純粋なメンバー関数からスレッドを開始することは可能ですか? そうでない場合、オブジェクトobjからfoo関数をラップして、そのようなスレッドを作成できるようにするにはどうすればよいですか? 前もって感謝します!
これはコンパイルエラーです:
thread_test.cpp: 関数 'int main()' 内: thread_test.cpp:32:22: エラー: 'std::thread::thread()' の呼び出しに一致する関数がありません</p>
thread_test.cpp:32:22: 注: 候補は:
/usr/include/c++/4.6/thread:133:7: 注意: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (A::*)(), _Args = {} ]
/usr/include/c++/4.6/thread:133:7: 注: 引数 1 の '' から 'void (A::*&&)()' への既知の変換はありません</p>
/usr/include/c++/4.6/thread:128:5: 注意: std::thread::thread(std::thread&&)
/usr/include/c++/4.6/thread:128:5: 注: 引数 1 の '' から 'std::thread&&' への既知の変換はありません</p>
/usr/include/c++/4.6/thread:124:5: 注意: std::thread::thread()
/usr/include/c++/4.6/thread:124:5: 注: 候補は 0 個の引数を想定しており、1 個が提供されています