将函数参数设置为具有多个参数的函数时出错

Errors with making a Function parameter a Function with multiple parameters

本文关键字:参数 函数 出错 设置      更新时间:2023-10-16

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

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

第一个功能:

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