在类中创建回调映射时出错

dch*_*tri 1 c++ boost

代码:

class Base{
  enum eventTypes{ EVENT_SHOW };
  std::map<int, boost::function<bool(int,int)> > m_validate;
  virtual void buildCallbacks();
  bool shouldShowEvent(int x, int y);
};
void Base::buildCallbacks(){
   m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

 In base.cxx
  return (p->*f_);
  Error: a pointer to a bound function may only be used to call
      the function (boundfuncalled)
Run Code Online (Sandbox Code Playgroud)

我得到了错误所说的内容,除了调用有界成员函数之外我不允许做任何其他事情,但我怎样才能绕过这个问题呢?我不确定为什么这不起作用.

Pet*_*ker 5

m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);
Run Code Online (Sandbox Code Playgroud)

调用bind()生成一个不带参数的函数对象.你不能使用这样的对象来调用Base::shouldShowEvent,因为它需要两个参数.所以你必须把函数对象变成一个带有两个参数的对象:

m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this, _1, _2);
Run Code Online (Sandbox Code Playgroud)

(未经测试......)