Boost.Bind非静态成员
问题描述:
我有以下代码,其中Boost.Local使用函数回调来加载mo文件。该函数对我来说叫做findMo,我试图将它绑定到一个对象上,这样我就可以保留我放在moFinder的私有成员中的副作用。Boost.Bind非静态成员
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;
// ...
}
编译时,我得到以下错误:
error: invalid use of non-static member function ‘std::vector moFinder::findMo(const std::string&, const std::string&)’
在那里我打电话的boost ::绑定就行了。
我在做什么值得这个错误?
答
您在成员地址前缺少运营商地址:&moFinder::findMo
。此外,您需要使用boost::mem_fn
将成员函数包装到函数对象中,并且缺少占位符。总而言之:
boost::bind(boost::mem_fn(&moFinder::findMo,), boost::ref(finder), _1, _2);
// or &finder
尽量减少您的问题,以较小的样本。这里没有什么与'boost.locale'有关的。它纯粹是“绑定”。这使得诊断更容易。 – pmr 2012-04-01 13:06:31
我通常会这样做,但我真的不确定它是否与Boost.Locale怪异有关。 – Jookia 2012-04-01 13:19:22