Boost 绑定非静态成员
我有以下代码,其中Boost.Local使用函数回调加载mo文件。这个函数对我来说叫做findMo,我正试图将它绑定到一个对象,这样我就可以保留我在moFinder的私有成员中添加的副作用Boost 绑定非静态成员,boost,boost-bind,boost-function,Boost,Boost Bind,Boost Function,我有以下代码,其中Boost.Local使用函数回调加载mo文件。这个函数对我来说叫做findMo,我正试图将它绑定到一个对象,这样我就可以保留我在moFinder的私有成员中添加的副作用 class moFinder { public: moFinder(string const& wantedFormat) : format(wantedFormat) { // ... } std::vector<char> fi
class moFinder
{
public:
moFinder(string const& wantedFormat)
: format(wantedFormat)
{
// ...
}
std::vector<char> findMo(std::string const& filePath, std::string const& encoding)
{
// ...
}
};
std::locale createLocale(string const& name, string const& customTranslation,
string const& path, string const& domain, string const& pathFormat)
{
// ...
namespace blg = boost::locale::gnu_gettext;
blg::messages_info info;
info.paths.push_back(path);
info.domains.push_back(blg::messages_info::domain(domain));
moFinder finder(pathFormat);
blg::messages_info::callback_type callbackFunc;
callbackFunc = boost::bind(moFinder::findMo, boost::ref(finder));
info.callback = callbackFunc;
// ...
}
编译时出现以下错误:
错误:非静态成员函数“std::vector moFinder::findMoconst std::string&,const std::string&”的使用无效
在我调用boost::bind的线路上
我做了什么才应该得到这个错误?在成员地址前面缺少运算符的地址:&moFinder::findMo。此外,您需要使用boost::mem_fn将成员函数包装到函数对象中,并且您缺少占位符。总而言之:
boost::bind(boost::mem_fn(&moFinder::findMo,), boost::ref(finder), _1, _2);
// or &finder
成员地址前面缺少运算符的地址:&moFinder::findMo。此外,您需要使用boost::mem_fn将成员函数包装到函数对象中,并且您缺少占位符。总而言之:
boost::bind(boost::mem_fn(&moFinder::findMo,), boost::ref(finder), _1, _2);
// or &finder
尽量把你的问题缩小到更小的样本。这里与boost.locale无关。这是纯粹的束缚。这使得诊断它们变得容易多了。我通常是这样做的,但老实说,我不确定这是否与Boost有关。语言环境怪异与否。尝试将您的问题减少到较小的样本中。这里与boost.locale无关。这是纯粹的束缚。这使诊断它们变得容易多了。我通常是这样做的,但我真的不确定这是否与Boost有关。区域设置是否奇怪。谢谢!我以前从未见过boost::mem_fn或_1、_2s,当查看其他示例时,我是否遗漏了什么?@Jookia the boost.bind documentation;谢谢我以前从未见过boost::mem_fn或_1、_2s,当查看其他示例时,我是否遗漏了什么?@Jookia the boost.bind documentation;