Static array of method pointers
-
26-01-2021 - |
Question
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.
Solution
void (Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
declares a global variable named packetHandler
. You want to define your class variable, which needs an extra Client::
like so:
void (Client::*Client::packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);
OTHER TIPS
Client::*packetHandler
is a member function pointer that points to a member function named Client::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 like boost::function
or std::tr1::function
or write your own member function wrapper class.