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

时间:2012-10-23 22:37:12

标签: 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);
}

我收到以下错误:

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

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

1 个答案:

答案 0 :(得分:5)

m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);

bind()的调用产生一个不带参数的函数对象。你不能使用这样的对象来调用Base::shouldShowEvent,因为它需要两个参数。因此,您必须将函数对象转换为带有两个参数的函数对象:

m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this, _1, _2);

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