首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >升压信号和传递类方法

升压信号和传递类方法
EN

Stack Overflow用户
提问于 2010-06-15 17:06:46
回答 3查看 5.2K关注 0票数 3

我定义了一些信号:

代码语言:javascript
复制
typedef boost::signals2::signal<void (int temp)> SomeSig;
typedef SomeSig::slot_type SomeSigType;

我有一些课:

代码语言:javascript
复制
class SomeClass
{
   SomeClass()
   {
     SomeSig.connect(&SomeClass::doMethod);
   }
   void doMethod(const SomeSig &slot);
};

犯了很多错误:

代码语言:javascript
复制
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:新代码(相同的错误):

代码语言:javascript
复制
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);
};
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-06-15 20:15:41

帕维尔和基思都是对的。SomeSig是一种类型,您不能调用类型。您必须实例化SomeSig。在使用方法函数指针时,还必须提供指向对象的指针。_1是绑定期间所需的位置保持器,指示绑定的方法函数指针需要一个参数。

代码语言:javascript
复制
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);
};
票数 5
EN

Stack Overflow用户

发布于 2010-06-15 17:20:13

使用助推::绑定。

代码语言:javascript
复制
SomeSig.connect(bind(&SomeClass::doMethod, this, _1));

问题是,您的信号需要一个隐藏的this指针,即对某个对象进行操作。或者,可以将指向静态方法的指针传递给它。

票数 3
EN

Stack Overflow用户

发布于 2010-06-15 17:23:18

SomeSig是一种类型,而不是对象。您需要定义一个信号对象并在该对象上调用connect。另外,是doMethod(),槽参数的类型是错误的。

代码语言:javascript
复制
class SomeClass
{
  SomeClass()
  {
    signal.connect(&SomeClass::doMethod);
  }
  void doMethod(const SomeSigType &slot);
private:
    SomeSig signal;

};

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3047381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档