views:

353

answers:

8

I'm getting a compile error (MS VS 2008) that I just don't understand. After messing with it for many hours, it's all blurry and I feel like there's something very obvious (and very stupid) that I'm missing. Here's the essential code:

typedef int (C::*PFN)(int);

struct MAP_ENTRY
    {
    int id;
    PFN pfn;
    };

class C
    {
    ...
    int Dispatch(int, int);
    MAP_ENTRY *pMap;
    ...
    };

int C::Dispatch(int id, int val)
    {
    for (MAP_ENTRY *p = pMap; p->id != 0; ++p)
        {
        if (p->id == id)
            return p->pfn(val);  // <--- error here
        }
    return 0;
    }

The compiler claims at the arrow that the "term does not evaluate to a function taking 1 argument". Why not? PFN is prototyped as a function taking one argument, and MAP_ENTRY.pfn is a PFN. What am I missing here?

+7  A: 

Try

return (this->*p->pfn)(val);
Michael Krelin - hacker
chrisd
A: 

p->pfn is a function pointer. You need to use * to make it function. Change to

(*(p->pfn))(val)
Sherwood Hu
+10  A: 

p->pfn is a pointer of pointer-to-member-function type. In order to call a function through such a pointer you need to use either operator ->* or operator .* and supply an object of type C as the left operand. You didn't.

I don't know which object of type C is supposed to be used here - only you know that - but in your example it could be *this. In that case the call might look as follows

(this->*p->pfn)(val)

In order to make it look a bit less convoluted, you can introduce an intermediate variable

PFN pfn = p->pfn;
(this->*pfn)(val);
AndreyT
Thanks. Now I not only have the correction, I understand why.
chrisd
A: 

I think it should be:

p->*pfn(val)
Francis Gagné
A: 

Do yourself a favor and use boost::function (and boost::bind too!)

Andreas Bonini
"Where does this scratch on my bicycle come from?" - "Do yourself a favor - buy an aircraft"
Michael Krelin - hacker
A: 

this is not answering your question, but consider using boost::function and boost::bind instead. It introduces a pretty big dependency into your codebase, but it's well worth it for larger projects.

George
A: 

Check out this section of the C++ FAQ, dedicated to your issue:
Click here for C++ FAQ section on Pointers To Members.

Thomas Matthews
A: 

Just to chime in with my own experience, I've come across an error in g++ caused by this statement:

  (this -> *stateHandler)() ;

Where stateHandler is a pointer to a void member function of the class referenced by *this. The problem was caused by the spaces between the arrow operator. The following snippet compiles fine:

(this->*stateHandler)() ;

I'm using g++ (GCC) 4.4.2 20090825 (prerelease). FWIW.

Gearoid Murphy