1

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.

9
  • statics are local to your translation unit. maybe an export would be in order. Commented Sep 22, 2011 at 19:57
  • 2
    would it be too obnoxious to tell you to use boost::function instead ? Commented Sep 22, 2011 at 19:58
  • 5
    I would strongly recommend a few typedefs to clean up that mess. Commented Sep 22, 2011 at 19:58
  • is there any chance few of the lines exists in a header file ? Commented Sep 22, 2011 at 20:02
  • @KerrekSB typedefs would make those cleaner, but not so funny. Commented Sep 22, 2011 at 20:08

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Haha thank you Neil. I was thinking of something like this after @thekashyap comment. I tried void (Client::Client::*packetHandler[Op::handledOpcodeMax - Op::handledOpcodeMin + 1])(QByteArray &data);. Bad luck with * position :D. Thanks :)
Just goes to show how important Kerrek SB's comment abt typedefing is.. :)
0

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.

1 Comment

To make a function pointer pointing to whatever member function you want is simple, you have nothing special to do. My problem here is that I want this array to be a static element of my class. Removing the static keyword in the header file makes the compilation and linking work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.