template <class T>
bool cmp(const T &a, const T &b){
return a <= b;
}
template <class T>
void bubble_sort(T tablica[], int size, bool compare(T,T)){
bool change = true;
while(change){
change = false;
for(int i=0; i < size-1; ++i){
if(compare(tablica[i+1], tablica[i])){
zamien(tablica[i+1], tablica[i]);
change = true;
}
}
}
}
它不起作用,我有错误:
'void bubble_sort(T [],int,bool (__cdecl *)(T,T))' :
could not deduce template argument for 'T []' from 'int [10]'
'void bubble_sort(T [],int,bool (__cdecl *)(T,T))' :
cannot use function template 'bool cmp(const T,const T)' as a function argument'
但是当我用它替换 cmp 函数时:
bool cmp(const int a, const int b){
return a <= b;
}
一切正常。如何更改我的 cmp 函数以使用模板?