注册 登录
编程论坛 C++教室

关于模板与函数指针结合和问题

lsrwan 发布于 2010-03-30 22:39, 705 次点击
模板功能强大,现在想和函数指针结合起来使用,总是报错,代码如下,望各位指点。
报错信息 Error    1    error C2782: 'bool bOnlyForCheck(_ty,_ty,_ty,_ty (__cdecl *)(_ty))' : template parameter '_ty' is ambiguous    e:\soft\vc++\test\temptest\temptest\temptest.cpp    25
我发现非要取代 函数头中的  _ty(*pfun)(_ty)) _ty的才能编译成功。


#include "stdafx.h"
#include<crtdbg.h>
template <class _ty>
bool bOnlyForCheck(_ty Llmt,_ty Hlmt, _ty var, _ty(*pfun)(_ty))
{
    if (Hlmt < Llmt)
    {
        _ASSERT(0);
    }
    _ty a = pfun(var);
    a = a >= Llmt && a <= Hlmt ? a : (a > Hlmt ? Hlmt : Llmt);
    return true;
}
float fTest(float fVar1)
{
    return fVar1*fVar1;
}

int _tmain(int argc, _TCHAR* argv[])
{
    bOnlyForCheck(1,2,3,fTest);
    bOnlyForCheck(1,0,3,fTest);
    return 0;
}
1 回复
#2
lsrwan2010-03-31 17:44
这个问题没人遇到过么?期待版主解答.
1