我试图将我的回调函数comp
传递给模板函数quickSortR
,但是出现了以下错误:
2 IntelliSense: no instance of function template "quickSortR", corresponding to the list of arguments"
当我取消代码顶部的注释时,我得到另一个错误列表。因此,我认为问题在于回调函数的使用不正确。
#include<stdio.h>
/*
template<class T>
int comp(const void*, const void*);
template<class T>
void quickSortR(T* a, long N, int comp(const void*,const void*));
Here is my instance. I commented it.
*/
template<class T>
int comp(const void* a, const void* b)
{
return (*(T*)a - *(T*)b);
}
template<class T>
void quickSortR(T* a, long N, int comp(const void*,const void*)) {
long i = 0, j = N;
T temp, p;
p = a[ N>>1 ];
do {
while ( !comp(*a[i],*p) ) i++;
while ( comp(*a[j],*p) ) j--;
if (i <= j) {
temp = a[i]; a[i] = a[j]; a[j] = temp;
i++; j--;
}
} while ( i<=j );
if ( j > 0 ) quickSortR(a, j, comp);
if ( N > i ) quickSortR(a+i, N-i, comp);
}
int main() {
int n;
int m[10] = {1,-3,5,-100,7,33,44,67,-4, 0};
//problem is on this line
quickSortR<int>(m, 10, comp);
for (n=0; n<10; n++)
printf ("%d ",m[n]);
return 0;
}