Как вызвать перегруженный метод в потоке std::thread?
Я новичок в потоковой обработке, я пытаюсь передать перегруженные методы в std::thread, как показано в примере ниже
#include <iostream>
#include <thread>
using namespace std;
class thA
{
public:
int fun(int a, int b, int c)
{
return a + b + c;
}
int fun()
{
cout << "Hello World" << endl;
return 0;
}
};
int main()
{
thA a;
thread classAAA ( &thA::fun, &a );
classAAA.detach();
while (true);
return 0;
}
но программа не компилируется и выдает ошибку:
main.cpp:21:37: error: no matching function for call to ‘std::thread::thread(, thA*)’
21 | thread classAAA ( &thA::fun, &a );
Есть ли способ вызвать перегруженные методы в потоке?
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Ну укажите конкретный тип вызываемой функции-члена...
using func = int (thA::*)(void);
thread classAAA( func(&thA::fun), a);
#include <iostream>
#include <thread>
volatile int run = true;
using namespace std;
class thA
{
public:
int fun(int a, int b, int c)
{
cout << "Farewell World" << endl;
run = false;
return a + b + c;
}
int fun()
{
cout << "Hello World" << endl;
run = false;
return 0;
}
};
int main()
{
thA a;
using func = int (thA::*)(int,int,int);
thread classAAA( func(&thA::fun), a, 0, 1, 2);
classAAA.detach();
while(run){}
return 0;
}