C ++ 11 Thread-Initialisierung mit Elementfunktionen Kompilierungsfehler [duplizieren]

Diese Frage hat hier bereits eine Antwort:

Start thread with member function 5 Antworten

Ich fange gerade an, C ++ 11-Threads zu verwenden, und habe mit einem (wahrscheinlich albernen) Fehler zu kämpfen. Das ist mein Beispielprogramm:

#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;
}

Ist es möglich, einen Thread von einer reinen Member-Funktion aus zu starten? Wenn nicht, wie kann ich mein @ verpacke foo Funktion von Objekt obj in der Lage sein, solche Thread zu erstellen? Danke im Voraus

Dies ist der Kompilierungsfehler:

thread_test.cpp: In der Funktion 'int main ()': thread_test.cpp: 32: 22: Fehler: Keine passende Funktion für den Aufruf von 'std :: thread :: thread ()'

thread_test.cpp: 32: 22: hinweis: kandidaten sind:

/ usr / include / c ++ / 4.6 / thread: 133: 7: Anmerkung: std :: thread :: thread (_Callable &&, _Args && ...) [mit _Callable = void (A :: *) (), _Args = { }]

/ usr / include / c ++ / 4.6 / thread: 133: 7: Anmerkung: Keine bekannte Konvertierung für Argument 1 von "" in "void (A :: * &&) ()"

/ usr / include / c ++ / 4.6 / thread: 128: 5: hinweis: std :: thread :: thread (std :: thread &&)

/ usr / include / c ++ / 4.6 / thread: 128: 5: Anmerkung: Keine bekannte Konvertierung für Argument 1 von "" in "std :: thread &&"

/ usr / include / c ++ / 4.6 / thread: 124: 5: note: std :: thread :: thread ()

/ usr / include / c ++ / 4.6 / thread: 124: 5: note: Kandidat erwartet 0 Argumente, 1 angegeben

Antworten auf die Frage(1)

Ihre Antwort auf die Frage