| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 701 人关注过本帖
标题:关于模板与函数指针结合和问题
只看楼主 加入收藏
lsrwan
Rank: 2
等 级:论坛游民
帖 子:31
专家分:17
注 册:2009-10-11
结帖率:90%
收藏
 问题点数:0 回复次数:1 
关于模板与函数指针结合和问题
模板功能强大,现在想和函数指针结合起来使用,总是报错,代码如下,望各位指点。
报错信息 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;
}
搜索更多相关主题的帖子: 模板 结合 函数 指针 
2010-03-30 22:39
lsrwan
Rank: 2
等 级:论坛游民
帖 子:31
专家分:17
注 册:2009-10-11
收藏
得分:0 
这个问题没人遇到过么?期待版主解答.
2010-03-31 17:44
快速回复:关于模板与函数指针结合和问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015029 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved