1

// 我将定义所有涉及的函数。

//首先是我要传递的排序函数。

void bubbleSort(vector<int> &vector);

//其次是调用函数的函数,该函数应该将函数作为参数。

int testSorts(vector<int> &vector, ofstream& outfile, string data)
{
    displaySort(vector, outfile, sortName, bubbleSort);
}

//这是应该将向量、文件和函数作为参数的函数。

// 当我运行它时,我得到以下错误。[错误] 模板参数 1 无效

void displaySort(vector<int>& vector, ofstream& outfile, string data, std::function<void 
(vector<int>& vector)> func)
{func(vector);}

//这是我从堆栈溢出中得到的代码。我想知道为什么我的没有。我做了同样的事情。

#include <functional>

double Combiner(double a, double b, std::function<double 
(double,double)> func){
  return func(a,b);
}

double Add(double a, double b){
  return a+b;
}

double Mult(double a, double b){
  return a*b;
}

int main(){
  Combiner(12,13,Add);
  Combiner(12,13,Mult);
}
4

1 回答 1

1

不要这样做using namespace std;并指定命名空间,它应该可以工作:

void displaySort(std::vector<int>& vector, std::ofstream& outfile,
                 std::string data, std::function<void(std::vector<int>&)> func) 
{
    func(vector);
}

如果您出于某种原因想要坚持使用,请在规范中using namespace std;添加::before :vectorfunction

void displaySort(vector<int>& vector, ofstream& outfile,
                 string data, function<void(::vector<int>&)> func)
//                                          ^^
{
    func(vector);
}
于 2021-09-26T20:30:19.123 回答