I compile the following code by
g++ -std=c++11 main.cpp
and I get the error:
main.cpp: In member function ‘void Object::que_action(double, double)’:
main.cpp:29:23: error: expected primary-expression before ‘(’ token
th[i] = std::thread(
^
What I am doing is that I am calling the function que_action
which is a template function and its parameter is a method from the same class. If this function was supposed to be called directly, (this->*param_function)(x,y)
would do the job. But, here I am passing it to a thread function which causes this error. How should I fix this error and make the application work?
#include <iostream>
#include <thread>
#include <cmath>
class Object
{
public:
void aaa(double x,double y)
{
std::cout<<"aaa action "<<x<<","<<y<<std::endl;
}
void bbb(double x,double y)
{
std::cout<<"bbb action "<<x<<","<<y<<std::endl;
}
template <void (Object::*param_function)(double x,double y)>
void que_action(double r, double theta)
{
double x = r * cos(theta);
double y = r * sin(theta);
constexpr int N = 8;
std::thread th[N];
for(int i = 0; i < N; i++)
{
th[i] = std::thread(
&(std::remove_reference<decltype(*this)>::type::*param_function),
this, double x, double y);
}
for(int i = 0; i < N; i++)
{
th[i].join();
}
}
void que_both_actions(double r, double theta)
{
que_action<&Object::aaa>(r, theta);
que_action<&Object::bbb>(r, theta);
}
};
int main()
{
Object obj;
obj.que_both_actions(5,3.14159/2.0);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire