Anyone knows how to compile this example code under msvc2010 ? (supposedly compiles under GCC)
class cmdLine;
struct cmdList
{
const char *c;
const char *s;
const char *h;
void (cmdLine::*cmdFuncPtr)();
};
class cmdLine
{
public:
cmdLine();
static cmdList myCmdList[];
void test();
};
cmdLine::cmdLine()
{
}
void cmdLine::test()
{
}
cmdList cmdLine::myCmdList[] =
{
{"xxx", "yyy", "zzzzz", &cmdLine::test},
{"aaa", "bbb", "ccc", 0}
};
int _tmain(int argc, _TCHAR* argv[])
{
cmdLine c;
(c.myCmdList[0].*cmdFuncPtr) (); //error (why?)
}
I get error C2065: ‘cmdFuncPtr’ : undeclared identifier and dont know whats wrong ?
Use this syntax
As
cmdFuncPtris a pointer to a method ofcmdLine, it needs an instance of the class to be invoked on, which isc. At the same time,cmdFuncPtris a member ofcmdList, so it needs an instance of the class where it is stored, which isc.myCmdList[0]. That’s whycshall be used twice in the expression.The expression presented by OP parses as: “Invoke a method on an instance of a class in
c.myCmdList[0]through a method pointer stored in a standalone variablecmdFuncPtr“. Such variable doesn’t exist, that’s what the compiler complains about.