请有人帮助解释为什么在OS X上使用Xcode 5.1编译以下代码时出现错误。 Apple LLVM 5.1版(clang-503.0.40)(基于在LLVM 3.4svn上)。构建std :: function向量时编译器出错
#include <vector>
#include <functional>
void func1(const std::string& value)
{
// ...
}
void func2(const std::string& value, int min, int max)
{
// ...
}
class X
{
public:
void x1(const std::string& value)
{
// ...
}
void x2(const std::string& value, int min, int max)
{
// ...
}
};
const std::vector<std::function<void(std::string)>> functions
{
func1,
std::bind(func2, std::placeholders::_1, 5, 6),
std::mem_fn(&X::x1), // compiler error
};
报告该错误是:
no matching constructor for initialization of 'const std::vector<std::function<void (std::string)> >'
const std::vector<std::function<void(std::string)>> functions
此外,我想X :: X2添加到载体。我会怎么做?
谢谢。
成员函数需要一个对象进行操作,他们的签名是不兼容与'void(std :: string)'在你的情况。你可以使用bind来使它工作,但你需要一个实例。 – Mat 2014-10-03 08:26:07
@Mat请添加此作为答案。 – balajeerc 2014-10-03 08:30:53
@Mat请你详细说明。 – ksl 2014-10-03 08:33:05