加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – tr1 :: function和tr1 :: bind

发布时间:2020-12-16 10:22:25 所属栏目:百科 来源:网络整理
导读:我将以下内容放入Ideone.com(和codepad.org): #include iostream#include string#include tr1/functionalstruct A { A(const std::string n) : name_(n) {} void printit(const std::string s) { std::cout name_ " says " s std::endl; }private: const st
我将以下内容放入Ideone.com(和codepad.org):

#include <iostream>
#include <string>
#include <tr1/functional>

struct A {
    A(const std::string& n) : name_(n) {}
    void printit(const std::string& s) 
    {
        std::cout << name_ << " says " << s << std::endl;
    }
private:
    const std::string name_;
};

int main()
{
    A a("Joe");
    std::tr1::function<void(const std::string&)> f = std::tr1::bind(&A::printit,&a,_1);
    a("Hi");
}

并得到这些错误:

prog.cpp: In function ‘int main()’:

prog.cpp:18: error: ‘_1’ was not declared in this scope

prog.cpp:19: error: no match for call to ‘(A)(const char [3])’

prog.cpp:18: warning: unused variable ‘f’

我不能为我的生活找出第18行的错误.

解决方法

两个错误:

> _1在命名空间std :: tr1 :: placeholders中定义.你需要使用namespace std :: tr1 :: placeholders;在main()中,或使用std :: tr1 :: placeholders :: _ 1.
>第19行应为f(“Hi”),而不是(“Hi”).

#include <iostream>
#include <string>
#include <tr1/functional>

struct A {
    A(const std::string& n) : name_(n) {}
    void printit(const std::string& s) 
    {
        std::cout << name_ << " says " << s << std::endl;
    }
private:
    const std::string name_;
};

int main()
{
    using namespace std::tr1::placeholders;  // <-------

    A a("Joe");
    std::tr1::function<void(const std::string&)> f = std::tr1::bind(&A::printit,_1);
    f("Hi");    // <---------
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读