【问题标题】:Boost.Bind non-static memberBoost.Bind 非静态成员
【发布时间】:2012-04-01 12:58:46
【问题描述】:

我有以下代码,其中 Boost.Local 使用函数回调来加载 mo 文件。该函数对我来说称为 findMo,我正在尝试将它绑定到一个对象,这样我就可以保留我放在 moFinder 的私有成员中的副作用。

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::findMo(const std::string&, const std::string&)'的使用无效

在我调用 boost::bind 的那一行。

我在做什么才应得这个错误?

【问题讨论】:

  • 尝试将您的问题减少到更小的样本。这里与boost.locale 无关。纯粹是bind。这使得诊断它们变得容易得多。
  • 我通常会这样做,但老实说我不确定这是否与 Boost.Locale 的怪异有关。

标签: boost boost-bind boost-function


【解决方案1】:

您在会员地址之前缺少运营商地址:&amp;moFinder::findMo。此外,您将需要使用 boost::mem_fn 将成员函数包装到函数对象中,并且您缺少占位符。总而言之:

boost::bind(boost::mem_fn(&moFinder::findMo,), boost::ref(finder), _1, _2);
                                               // or &finder 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 2016-05-20
  • 2012-10-20
  • 2013-11-14
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多