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?
p->pfnis 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 typeCas the left operand. You didn’t.I don’t know which object of type
Cis 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 followsIn order to make it look a bit less convoluted, you can introduce an intermediate variable