使用qsort时指向模板函数的指针 [英] pointer to template function when using qsort

查看:88
本文介绍了使用qsort时指向模板函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我要使用QSort函数,它的参数之一是sort函数的指针,问题是当我调用模板sort函数时

排序功能:

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:

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

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;


}



使用时:
qsort(ResultNorm_Ref,32,sizeof(long double),sortFunction1);

但是使用时:
qsort(ResultNorm_Ref,32,sizeof(long double),sortFunction2< long double>));
我收到此错误:



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 :

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



以及使用此方法时:
qsort(ResultNorm_Ref,32,sizeof(long double),< long double> sortFunction2);

我收到此错误:错误C2059:语法错误:''<''

如何使用qsort函数调用模板排序函数?

谢谢



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

推荐答案

您使用的是哪个编译器?以下代码可用于VS2008.

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

#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



-Saurabh


我没有VS 2005,但是cPallini在VS2005上测试了此代码,对他来说很好用.您能否检查项目->"下的呼叫约定?属性->配置属性-> C/C ++->高级设置为_cdecl(/Gd).

-Saurabh
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


嗨 感谢您的尝试,但我仍然收到错误消息:
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.我在MFC项目中使用vs2005,qsort在stdlib.h下.
2.



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


这篇关于使用qsort时指向模板函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆