【发布时间】:2012-06-28 18:38:53
【问题描述】:
我将以下内容放入 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:在函数“int main()”中:
prog.cpp:18: 错误:'_1' 未在此范围内声明
prog.cpp:19: error: no match for call to ‘(A)(const char [3])’
prog.cpp:18:警告:未使用的变量“f”
我终其一生都无法弄清楚第 18 行出了什么问题。
【问题讨论】:
-
试试
std::tr1::placeholders::_1。 -
那里也需要一个 tr1::。我想我最近才使 tr1 占位符和 c++11 占位符相互兼容
-
@JonathanWakely,哎呀,从未使用过 tr1 的。我忘了。