以另一个函数为参数的函数“无匹配函数错误” C ++

弗洛伊德Vu0531

我正在编写一系列排序算法,并计算每种算法的执行时间。

#include <iostream>
#include <vector>
#include <chrono>
#include <utility>


#include "sort.h"


template<class T>
void timeTaken(void (*f)(std::vector<T>& nums))
{
    auto t1 = std::chrono::high_resolution_clock::now();
    f();
    auto t2 = std::chrono::high_resolution_clock::now();

    double duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();

    std::cout << duration << " microseconds";
    
   // return duration;
}



template<typename T>
void display(std::vector<T>& nums)
{
    for(int x : nums) std::cout << x << "  ";
    std::cout << "\n\t ---------------------------------- \n";
}

void displayTesting()
{
    std::cout << "testing .... \n";
}

int main()
{
   
    std::vector<int> A {4,5,2,7,1,10,15};
    
    std::cout << "ORIGNAL ARRAY: ";
    
    display(A);
    
    

    bubbleSort(A);
    timeTaken(&display);        //ERROR: No matching function for call "time Taken"
    std::cout << "BUBBLE SORT: ";
    display(A);
              
  
    


    
    
    std::cout << "\n";
    return 0;
}

当我尝试将displayTesting()作为timeTaken()的参数时,它工作正常(当然,我将after(* f)删除了该参数)。所以我认为问题基本上是我如何将某个函数的参数带入timeTaken()函数中。

songyuanyao

display是模板;T无法在中推导模板参数timeTaken(&display);

您可以通过指定实例化static_cast

timeTaken(static_cast<void (*)(std::vector<int>&)>(&display));

要么

timeTaken(&display<int>);

或显式指定模板参数。

timeTaken<int>(&display);

其他问题:

  1. 没有bubbleSort
  2. f();正在尝试f不带参数的调用,但应该接受std::vector<T>&

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从另一个函数错误C调用函数

提升python C ++函数调用另一个函数错误

C# 函数错误,因为无法重定向到另一个页面

Dapper C#:无参数的默认构造函数或一个匹配的签名错误

将C ++函数用作导出的Rcpp函数调用的另一个C ++函数的参数

C ++退出另一个函数的函数

另一个函数内部的C ++调用函数

如何将函数的结果用作C中另一个函数的参数?

调用另一个函数内具有参数类型的函数c#

将具有不同数量参数的函数传递给另一个函数C ++

C ++将函数的参数传递给另一个函数

如何在C ++中将可变参数函数与另一个函数消除歧义

另一个使用引用和参数的函数中的C ++调用函数

从另一个函数C#调用另一个函数

将一个函数传递给C ++中的另一个函数,其中参数的数量可以不同

在c++中使用一个函数的返回值作为另一个函数的参数

将指向一个C函数的指针作为另一个函数的参数传递

如何创建一个返回另一个函数的函数,在C中隐式包含给定参数?

构造函数中的 C++ 语法错误 - 参数是对来自另一个类的对象的引用

在模板函数C ++中的F &&,Args && ...参数之后添加另一个函数作为参数

具有可变数量和参数类型的C ++函数作为另一个函数的参数

从另一个文件调用一个函数到另一个函数C ++

将一个函数模板的模板参数映射到另一个 (c++) 的模板参数

C ++将函数参数传递给另一个lambda

C ++使用移动语义将参数传递给另一个函数

使用另一个QML对象作为参数从C ++调用QML函数

C宏数据库-在另一个宏中测试宏函数的参数

C ++中带有另一个模板类作为参数的构造函数

C++,在另一个对象构造函数中将对象作为参数传递