函数模板问题(带一个函数指针参数)
主要就是想实现一个通用的排序函数:
void QuickSort( type * front, type * end, int(*compare)( const type & var1, const type & var2) )
前两个参数指向需要比较的头尾两个指针,第三个为一个函数指针,指向一个用于比较元素的函数(元素可以是整形、字符......),若var1小与var2则返回-1,大于返回1,等于返回0。假设现在对一个string数组按每个字符串长度进行排序,先给出一个比较函数:
int SizeCompare( const string & st1, const string & st2 ){
if( st1.size() == st2.size() )
return 0;
else
return (st1.size() < st2.size()) ? -1:1;
}
然后定义一个字符串数组string test[10];
调用排序函数:
QuickSort( test, test + sizeof( test )/sizeof( test[0] ) - 1, SizeCompare );
可是vc6编译时总是出错:
error C2782: 'void __cdecl QuickSort(type *,type *,int (__cdecl *)(const type &,const type &))' : template parameter 'type' is ambiguous
哪位高手指点一下,感激不尽!