【问题标题】:Error passing a function in as a parameter C++将函数作为参数传递 C++ 时出错
【发布时间】:2021-10-23 05:32:25
【问题描述】:

我有一个测试搜索方法的函数时间的工作实现,它需要:

checkSearchTime(T(*funcPointer) (T myArray[], int size, T wanted)

作为参数,我尝试用排序方法做同样的事情:

checkSortTime(T(*funcPointer) (T myArray[],int size)

我得到一个错误。这是错误:

Error   C2664   'void checkSortTime<int>(T (__cdecl *)(T [],int),T [],int)': cannot convert argument 1 from 'void (__cdecl *)(T [],int)' to 'T (__cdecl *)(T [],int)'   

这是两个文件的代码:

int main() {
    memLeaks();
    constexpr auto SIZE = 5;
    int myArray[SIZE];
    populateArrayRandom(myArray, SIZE);
    PrintArray(myArray, SIZE);
    //insertionSort(myArray, SIZE);
    PrintArray(myArray, SIZE);
    
    checkSearchTime(binary_search, myArray, SIZE, 12);
    checkSortTime(selectionSort, myArray, SIZE); // THIS IS WHERE THE ERROR IS

    //checkAllSorts(myArray, SIZE); // WOULD LIKE TO CALL THIS AFTER
}


template<typename T>   
void checkSearchTime(T(*funcPointer) (T myArray[], int size, T wanted),
    T arrayArgument[], int sizeArgument, T wantedArgument) {
    
    // Use auto keyword to avoid typing long
    auto start = std::chrono::high_resolution_clock::now();
    funcPointer(arrayArgument, sizeArgument, wantedArgument);
    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);

    //std::cout << "Microseconds: " << duration.count() << std::endl;
    std::cout << "[" << duration.count() << "] Nanoseconds" << std::endl;
}

template<typename T>                                                             
void checkAllSearches(T myArray[], int size, T value) {
    std::cout << "Search Implementations and Timing\n";
    std::cout << "Binary=";
    checkSearchTime(binary_search, myArray, size, value);
    std::cout << "Linear=";
    checkSearchTime(linear_search, myArray, size, value);
    std::cout << "JumpSearch=";
    checkSearchTime(jump_search, myArray, size, value);
    std::cout << "Exponential=";
    checkSearchTime(exponential_search, myArray, size, value);
    std::cout << "FibMonaccian=";
    checkSearchTime(fibMonaccian_search, myArray, size, value);
}

template<typename T>                                                                                
void checkSortTime(T(*funcPointer) (T myArray[],int size), //tried const here, doesn't work either
    T arrayArgument[], int sizeArgument) {
    // Use auto keyword to avoid typing long
    auto start = std::chrono::high_resolution_clock::now();
    funcPointer(arrayArgument, sizeArgument);
    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);

    //std::cout << "Microseconds: " << duration.count() << std::endl;
    std::cout << "[" << duration.count() << "] Nanoseconds" << std::endl;
}

【问题讨论】:

    标签: c++ function header void


    【解决方案1】:

    正如您已经知道的那样,您的排序函数 (selectionSort) 的返回类型必须是 void

    所以,我只是将这个答案发布给可能面临类似问题的其他人。

    改变这个

    template<typename T>                                                                                
    void checkSortTime(T(*funcPointer)
    

    到以下,将解决问题:

    void checkSortTime(void(*funcPointer)
    

    【讨论】:

      【解决方案2】:

      这是因为排序方法不返回任何内容。

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:

      不是真正的答案,只是为了让您知道: 通过“const T(&name)[const size]”传递数组,保持数组的大小。 并且你保证不会改变它的内容(const)。 这种语法也可以用在这样的模板中(在这个例子中我只是返回值,没有更新为返回 void):

      #include <iostream>
      
      template<typename T, std::size_t N>
      T checkSearchTime(T(*funcPointer)(const T(&myArray)[N], const T& wanted), const T (&arrayArgument)[N], const T& wantedArgument)
      {
          return funcPointer(arrayArgument, wantedArgument);
      }
      
      template<std::size_t N>
      int lookup(const int(&arr)[N], const int& value)
      {
          for (int i = 0; i < N; ++i) std::cout << i << " ";
          std::cout << "\n";
          return value;
      }
      
      int main()
      {
          int arr[]{ 1,2,3,4,5 };
          int wanted = 3;
          int value = checkSearchTime(lookup, arr, wanted);
          std::cout << "value = " << value;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-14
        • 2021-02-10
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多