方法指针的静态数组
我面临一个我不明白的错误。
我尝试创建并使用方法指针的静态数组。我的类中的声明如下所示:
static void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
在我的 .cpp 文件中进行初始化是这样的:
void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
麻烦来了,在我的 Client 类方法之一中,我尝试使用此方法指针的数组。 我尝试了几种方法,例如:
(this->*packetHandler[_opcode])(data);
我说我不明白这个问题,让我解释一下原因。在我的代码上运行 make 会导致正确的编译,但链接时出现问题。
client.cpp:71: undefined reference to `Client::packetHandler'
此消息重复 5 次。
欢迎任何帮助。 谢谢。
I'm facing an error I dont understand.
I try to create and use a static array of method pointers. The declaration in my class looks like this:
static void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
Initialization takes place in my .cpp files is like that:
void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
Here comes the troubles, in one of my Client's class method I try to use this methods pointers' array.
I tried several ways, for example :
(this->*packetHandler[_opcode])(data);
I said I didnt understand the problem, let me explain why. Running make on my code results in a proper compilation, tought, there is a problem when linking.
client.cpp:71: undefined reference to `Client::packetHandler'
This message is repeated 5 times.
Any help would be welcome.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
声明一个名为packetHandler
的全局变量。您想要定义类变量,它需要一个额外的Client::
,如下所示:void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
declares a global variable namedpacketHandler
. You want to define your class variable, which needs an extraClient::
like so:Client::*packetHandler
是一个成员函数指针,它指向名为Client::packetHandler
的成员函数。我不确定如何创建一个指向任意成员函数的成员函数指针,这似乎是您想要做的。我认为乔治是对的。您应该考虑使用诸如boost::function
或std::tr1::function
之类的东西,或者编写自己的成员函数包装类。Client::*packetHandler
is a member function pointer that points to a member function namedClient::packetHandler
. I'm not sure how to make a member function pointer that points to an arbitrary member function which is what you seem to want to do. I think George is right. You should consider using something likeboost::function
orstd::tr1::function
or write your own member function wrapper class.