views:

209

answers:

4

I am trying to use a function pointer, but the 3 lines below just do not seem to want to cooperate...

I'm getting error code C3867.

Can you see what I'm doing wrong?

In .h file

void MyFunc(int, FILEINFO*(*)(FILEINFO*), FILEINFO*, int);

The definition in the .cpp file

void MyFunc(int number, FILEINFO*(*GetFiles)(FILEINFO*), FILEINFO* args, int type);

Then here is where I'm actually calling the function

MyFuncClass->MyFunc(GetNumber(), &BigClass::PassThis, GetArgs(), TheType);

Any problems jump out?

+9  A: 

What is the definition of BigClass::PassThis()? Is it a static class member function or a regular member function? If it's a regular member function, you can't do that, because it has a hidden this parameter. See question 33.4 of the C++ FAQ Lite.

Adam Rosenfield
+2  A: 

We'd need to see the definition of BigClass. Unless PassThis is a static member function, what you've done can't work because regular member functions can't be passed as a function pointer the way you are doing it (among other things, you've got no 'this' parameter).

Michael Kohne
+2  A: 

I'm guessing that &BigClass::PassThis is a pointer to a member function and not a pointer to an ordinary function.

james woodyatt
james woodyatt
+7  A: 

You cannot pass a non-static member function of a class as an ordinary function pointer, since a member function implicitly uses the this-pointer. A solution for this is to define a static member function that takes a pointer to the class as it's first argument and wraps the call to BigClass::PassThis and pass a pointer to that member function instead. Please see The Function Pointer Tutorials for more information.

A better solution might be to look into using functors instead.

danvari
Your solution worked out nicely. Thanks!
samoz
Also take a look into boost::function, boost::bind for other solutions to the same problem.
David Rodríguez - dribeas