使函数参数成为具有多个参数的函数的错误

Errors with making a Function parameter a Function with multiple parameters

我正在尝试使函数参数成为具有多个参数的函数

第一个函数的参数是一个只有一个参数的函数

第一个函数:

void execute_and_time(const string& method_name, double(method)(double), double num)

第二个函数的参数是一个有 2 个参数的函数,这会导致如下错误:

prog.cpp:50:65: error: expected ',' or '...' before '(' token
 void execute_and_time2(const string& method_name, double(method)((double),(double)), double num, double p) {

第二个函数:

void execute_and_time2(const string& method_name, double(method)((double),(double)), double num, double p)

当我这样写时它对我有用:

void execute_and_time(const string& method_name, double(method)(double), double num)
{
    double test = method(num);
}
void execute_and_time2(const string& method_name, double(method)(double,double), double num, double p)
{
    double test = method(num, p);
}

您似乎需要删除函数参数类型周围的额外括号(即 double)。

不过,您可能应该将函数参数写成实际的函数指针,如下所示:

void execute_and_time(const string& method_name, double(*method)(double), double num)
{
    double test = method(num);
}
void execute_and_time2(const string& method_name, double(*method)(double,double), double num, double p)
{
    double test = method(num, p);
}

备注*method