Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i what to use the QSort function and one of it's arguments is the pointer of the sort function, the problem is when i calling the template sort function

The sort function:
C++
template <class VAL_TYPE> int sortFunction2(const void *arg1,const void *arg2)
{
    VAL_TYPE d1 = (VAL_TYPE) arg1;
    VAL_TYPE d2 = (VAL_TYPE) arg2;

    if(*d1>*d2) return 1;
    if(*d1<*d2) return -1;
    return 0;


}


when using this function when using long double every thing ok

C++
int sortFunction1(const void *arg1,const void *arg2)
{
    long double *d1 = (long double *) arg1;
    long double *d2 = (long double *) arg2;

    if(*d1>*d2) return 1;
    if(*d1<*d2) return -1;
    return 0;


}


when using:
qsort(ResultNorm_Ref,32,sizeof(long double),sortFunction1);

but when using:
qsort(ResultNorm_Ref,32,sizeof(long double),sortFunction2<long double>);
i getting this error :
VB
error C2664: 'qsort' : cannot convert parameter 4 from 'int (const void *,const void *)' to 'int (__cdecl *)(const void *,const void *)'
        None of the functions with this name in scope match the target type


and when using this:
qsort(ResultNorm_Ref,32,sizeof(long double),<long double>sortFunction2);

i getting this error: error C2059: syntax error : '<'

how can i call the template sort function using the qsort function?

Thanks
Posted
Updated 10-Apr-10 23:13pm
v3

Which compiler are you using? Following code works with VS 2008.

C++
#include <cstdlib>

template <class VAL_TYPE>
int sortFunction2(const void *arg1,const void *arg2)
{
    VAL_TYPE* d1 = (VAL_TYPE*)arg1;
    VAL_TYPE* d2 = (VAL_TYPE*)arg2;
    if(*d1>*d2) return 1;
    if(*d1<*d2) return -1;
    return 0;
}

int main(int argc, char* argv[])
{
    long double x[8] = {10, 9, 8, 7, 6, 5, 4, 3};
    qsort(x, 8, sizeof(long double), sortFunction2<long double>);
    return 0;
}


-Saurabh
 
Share this answer
 
I don't have VS 2005 but cPallini tested this code on VS2005 and it works fine for him. Can you check that Calling Conventions under Project -> Properties -> Configuration Properties -> C/C++ -> Advanced is set to _cdecl(/Gd).

-Saurabh
 
Share this answer
 
Hi Thanks for the reply i tried it but i am still getting error:
'qsort' : cannot convert parameter 4 from 'int (const void *,const void *)' to 'int (__cdecl *)(const void *,const void *)'


1. I am using the vs2005 in MFC project the qsort is under stdlib.h.
2. could it be something with the
(__cdecl *)
? what it does any way?

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900