[英]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;
}
Nvm 这是因为排序方法不返回任何东西 =)
As you have already figured it out, the return type of your sort function ( selectionSort
) must have been void
.正如您已经知道的那样,您的排序函数 ( selectionSort
) 的返回类型必须是void
。
So, I am just posting this answer for others that might face a similar issue.所以,我只是为其他可能面临类似问题的人发布这个答案。
Changing this改变这个
template<typename T>
void checkSortTime(T(*funcPointer)
to the following, will solve the issue:到以下,将解决问题:
void checkSortTime(void(*funcPointer)
Not really an answer, but just to let you know: passing arrays by "const T(&name)[const size]" keeps the size with the array.不是真正的答案,只是让您知道:通过“const T(&name)[const size]”传递数组保持数组的大小。 And you promise not to change its content (const).并且您承诺不会更改其内容 (const)。 This syntax can also be used in templates like this (In this example I just return the value, didn't update to return void):这个语法也可以用在这样的模板中(在这个例子中我只返回值,没有更新以返回 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;
}
Happy coding快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.