免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1642 | 回复: 2
打印 上一主题 下一主题

[C++] std::mem_fun 编译出错。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-08 22:44 |只看该作者 |倒序浏览
本帖最后由 sytpb 于 2015-04-08 22:49 编辑

   
class TestClass1{
   
public:
    TestClass1(int d):data(d)
    {}
   
    bool test()
    {
        return data>3;
    }
private:
    int data;
};

    TestClass1 *t1 = new TestClass1(1);
    TestClass1 *t2 = new TestClass1(2);
    TestClass1 *t3 = new TestClass1(4);
    TestClass1 *t4 = new TestClass1(5);
   
    std::vector<TestClass1*> lpt;
    lpt.push_back(t1);
    lpt.push_back(t2);
    lpt.push_back(t3);
    lpt.push_back(t4);
    std::vector<TestClass1*>::iterator it = std::find_if(lpt.begin(),lpt.end(),std::mem_fun(&TestClass1::test));    //出错此行

如果test 如下
    bool test(int a)
    {
        return data>a;
    }

std::vector<TestClass1*>::iterator it = std::find_if(lpt.begin(),lpt.end(),std::bind1st(std::mem_fun(&TestClass1::test),3));  

也没试对。请指教!

第一个find_if 出错信息
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:859:13: No matching function for call to object of type 'std::__1::mem_fun1_t<bool, TestClass1, int>'

论坛徽章:
0
2 [报告]
发表于 2015-04-08 22:45 |只看该作者
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:859:13: No matching function for call to object of type 'std::__1::mem_fun1_t<bool, TestClass1, int>'

第一个 find_if 出错信息.

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
3 [报告]
发表于 2015-04-09 08:50 |只看该作者
std::vector<TestClass1*>::iterator it = std::find_if(lpt.begin(),lpt.end(),std::mem_fun(&TestClass1::test));    //出错此行
------ 我用vc++2008和gcc4.8.1都能编译并运行成功。

std::bind1st(std::mem_fun(&TestClass1::test),3));
------ 应该是 std::bind2nd(std::mem_fun(&TestClass1::test2),3)

BTW:bool test() 后面加 const 更好
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP