Question

I have the following code, in which Boost.Local uses a function callback to load a mo file. The function is called findMo for me, and I'm trying to bind it to an object so I can retain the side effects I put in private members of 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;

  // ...
}

When compiling I get the following error:

error: invalid use of non-static member function ‘std::vector moFinder::findMo(const std::string&, const std::string&)’

At the line where I call boost::bind.

What am I doing to deserve this error?

Was it helpful?

Solution

You are missing the address of operator before the member address: &moFinder::findMo. Additionally you will need to use boost::mem_fn to wrap the member function into a function object and you are missing the placeholders. All-in-all:

boost::bind(boost::mem_fn(&moFinder::findMo,), boost::ref(finder), _1, _2);
                                               // or &finder 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top