請有人幫助解釋爲什麼在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