不能将函数 模板“void selectionSort(T [],int)”用作函数参数
不能将函数 模板“void selectionSort(T [],int)”用作函数参数
分别定义如下:
template<typename T>
void SortTime( string sortName,void(*sort)(T[],int), T arr[],int n)
{
clock_t startTime = clock();
sort(arr ,n);
clock_t endTime = clock();
assert( isSorted(arr , n));
cout<<sortName << " :" <<double(endTime-startTime) /CLOCKS_PER_SEC <<" s" <<endl;
return;
}
template<typename T>
void selectionSort(T arr[], int n){
for(int i = 0 ; i < n ; i ++){
int minIndex = i;
for( int j = i + 1 ; j < n ; j ++ )
if( arr[j] < arr[minIndex] )
minIndex = j;
swap( arr[i] , arr[minIndex] );
}
}
调用如下:
SortHelper::SortTime("Selection Sort", selectionSort,arr,n);
开始报错:1>c:\algorithm\selectionsort\selectionsort\main.cpp(28): error C2896: “void SortHelper::SortTime(std::string,void (__cdecl *)(T [],int),T [],int)”: 不能将函数 模板“void selectionSort(T [],int)”用作函数参数1> c:\algorithm\selectionsort\selectionsort\main.cpp(8) : 参见“selectionSort”的声明1>c:\algorithm\selectionsort\selectionsort\main.cpp(28): error C2784: “void SortHelper::SortTime(std::string,void (__cdecl *)(T [],int),T [],int)”: 未能从“重载函数类型”为“重载函数类型”推导 模板 参数
调用修改为SortHelper::SortTime("Selection Sort", selectionSort<int>,arr,n);后,再次报错
MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup1>F:\C++程序\Selection_Sort(2)\Debug\Selection_Sort(2).exe : fatal error LNK1120: 1 unresolved externals
请问是为什么呢?