我在组件UpdateComponent中使用UpdateComponent。此组件的特定聚合类型为Updateable。我希望Updateable能够连接到UpdateComponent的boost::signals2::signal。我应该指出,Updateable的slot是pure-virtual。
下面是代码的一个具体示例:
// This is the component that emits a boost::signals2::signal.
class UpdateComponent {
public:
UpdateComponent();
boost::signals2::signal<void (float)> onUpdate; // boost::signals2::signal
}在UpdateComponent代码的某个时候,我执行onUpdate(myFloat);我认为这类似于向所有“侦听器”“触发”boost::signals2::signal。
// The is the aggregate that should listen to UpdateComponent's boost::signals2::signal
class Updateable {
public:
Updateable();
protected:
virtual void onUpdate(float deltaTime) = 0; // This is the pure-virtual slot that listens to UpdateComponent.
UpdateComponent* m_updateComponent;
}在Updateable的构造函数中,我执行以下操作:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(&onUpdate);
}我收到以下两个错误:
...Updateable.cpp:8: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&BalaurEngine::Traits::Updateable::onUpdate' [-fpermissive]/usr/include/boost/function/function_template.hpp:225: error: no match for call to '(boost::_mfi::mf1<void, BalaurEngine::Traits::Updateable, float>) (float&)'我应该提到,我正在使用Qt与boost一起使用。但是,我已经将CONFIG += no_keywords添加到了我的.pro文件中,因此这两者应该能够顺利地一起工作,就像boost网站所概述的那样。我不使用Qt的signals和slots (效果很好)的原因是:我不希望Updateable成为QObject。
如果有人能帮我找出我为什么会出错,我会非常感激的!
发布于 2012-05-06 16:49:31
您要传递给connect的槽必须是函子。要连接到成员函数,可以使用boost::bind或C++11 lambda。例如,使用lambda:
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(
[=](float deltaTime){ onUpdate(deltaTime); });
}或者使用bind
Updateable::Updateable {
m_updateComponent = new UpdateComponent();
m_updateComponent->onUpdate.connect(
boost::bind(&Updateable::onUpdate, this, _1));
}https://stackoverflow.com/questions/10472216
复制相似问题