我将以下内容放入Ideone.com(和codepad.org):
#include
#include
#include
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 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
#include
#include
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 f = std::tr1::bind(&A::printit, &a, _1);
f("Hi"); //
}