我定义了一些信号:
\n\ntypedef boost::signals2::signal<void (int temp)> SomeSig;\ntypedef SomeSig::slot_type SomeSigType;\nRun Code Online (Sandbox Code Playgroud)\n\n我有一些课:
\n\nclass SomeClass\n{\n SomeClass()\n {\n SomeSig.connect(&SomeClass::doMethod);\n }\n void doMethod(const SomeSig &slot);\n};\nRun Code Online (Sandbox Code Playgroud)\n\n并且出现了很多错误:
\n\nerror: \xe2\x80\x98BOOST_PP_ENUM_SHIFTED_PARAMS_M\xe2\x80\x99 was not declared in this scope\nerror: \xe2\x80\x98T\xe2\x80\x99 was not declared in this scope\nerror: a function call cannot appear in a constant-expression\nerror: a function call cannot appear in a constant-expression\nerror: template argument 1 is invalid\nerror: \xe2\x80\x98BOOST_SIGNALS2_MISC_STATEMENT\xe2\x80\x99 has not been declared\nerror: expected identifier before \xe2\x80\x98~\xe2\x80\x99 token\nerror: expected \xe2\x80\x98)\xe2\x80\x99 before \xe2\x80\x98~\xe2\x80\x99 token\nerror: expected \xe2\x80\x98;\xe2\x80\x99 before \xe2\x80\x98~\xe2\x80\x99 token\nRun Code Online (Sandbox Code Playgroud)\n\nUPD :\n新代码(相同的错误):
\n\ntypedef boost::signals2::signal<void (int keyCode)> SigKeyPressed;\ntypedef SigKeyPressed::slot_type SigKeyPressedType;\n\nclass SomeClass\n{\n SigKeyPressed mSigKeyPressed;\n\n public:\n SomeClass() { mSigKeyPressed.connect(&SomeClass::keyPressed); }\n void keyPressed(const SigKeyPressedType &slot);\n};\nRun Code Online (Sandbox Code Playgroud)\n
帕维尔和基思都是正确的(两者都是+1)。SomeSig 是一种类型,您不能调用类型。您必须实例化 SomeSig。使用方法函数指针时,还必须提供指向对象的指针。_1 是绑定期间所需的占位符,指示所绑定的方法函数指针需要单个参数。
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);
};
Run Code Online (Sandbox Code Playgroud)