开发者

Boost signals and passing class method

I've defined some signal:

typedef boost::signa开发者_开发百科ls2::signal<void (int temp)> SomeSig;
typedef SomeSig::slot_type SomeSigType;

I have some class:

class SomeClass
{
   SomeClass()
   {
     SomeSig.connect(&SomeClass::doMethod);
   }
   void doMethod(const SomeSig &slot);
};

And got a lot of errors:

error: ‘BOOST_PP_ENUM_SHIFTED_PARAMS_M’ was not declared in this scope
error: ‘T’ was not declared in this scope
error: a function call cannot appear in a constant-expression
error: a function call cannot appear in a constant-expression
error: template argument 1 is invalid
error: ‘BOOST_SIGNALS2_MISC_STATEMENT’ has not been declared
error: expected identifier before ‘~’ token
error: expected ‘)’ before ‘~’ token
error: expected ‘;’ before ‘~’ token

UPD: New code (the same error):

typedef boost::signals2::signal<void (int keyCode)> SigKeyPressed;
typedef SigKeyPressed::slot_type SigKeyPressedType;

class SomeClass
{
        SigKeyPressed mSigKeyPressed;

    public:
        SomeClass() { mSigKeyPressed.connect(&SomeClass::keyPressed); }
        void keyPressed(const SigKeyPressedType &slot);
};


Both Pavel and Keith are correct (+1 for both). SomeSig is a type, you cannot call on a type. You must instantiate SomeSig. You also must provide a pointer to the object when using method function pointers. The _1 is a place holder required during a bind indicating that the method function pointer being bound requires a single argument.

typedef boost::signals2::signal<void (int keyCode)> SigKeyPressed;
typedef SigKeyPressed::slot_type SigKeyPressedType;

class SomeClass
{
        SigKeyPressed mSigKeyPressed;

    public:
        SomeClass() { mSigKeyPressed.connect(boost::bind(&SomeClass::keyPressed, this, _1); }
        void keyPressed(const SigKeyPressedType &slot);
};


Use boost::bind.

SomeSig.connect(bind(&SomeClass::doMethod, this, _1));

The thing is, that your signal requires a hidden this pointer, i.e. to act on some object. Alternatively, you can pass it a pointer to static method.


SomeSig is a type, not an object. You need to define a signal object and call connect on that object. Also, is doMethod(), the type of the slot parameter was wrong.

class SomeClass
{
  SomeClass()
  {
    signal.connect(&SomeClass::doMethod);
  }
  void doMethod(const SomeSigType &slot);
private:
    SomeSig signal;

};

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜