Search code examples
c++functionheadervoid

Error passing a function in as a parameter C++


I have a working implementation of testing the time of a function for search methods, which take:

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

as parameters, I try to do the same thing with sort methods:

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

and I get an error. This is the error:

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)'   

Here is the code for both files:

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;
}

Solution

  • It was because sort methods don't return anything.