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"); } 并得到这些错误:
我不能为我的生活找出第18行的错误. 解决方法
两个错误:
> _1在命名空间std :: tr1 :: placeholders中定义.你需要使用namespace std :: tr1 :: placeholders;在main()中,或使用std :: tr1 :: placeholders :: _ 1. #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"); // <--------- } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |