Array of method pointers

I've read a number of topics about function pointers, and many of them appear to be exactly what I'm trying to do, but for some reason, I can't get what I want to do to work. Basically, it won't compile.

Here's my simple class:

class LessSimple
{
public:
LessSimple ();
~LessSimple () {}

int doFunc (const char*, int);

private:
typedef int (*mthd)(int);

struct methodPtrs
{
const char *name;
mthd method;
};

methodPtrs methods[20];
int value1;
int value2;
int value3;

int method1 (int);
int method2 (int);
int method3 (int);
};

/*------------------------------------------------------------------------
* LessSimple::LessSimple
*------------------------------------------------------------------------*/

LessSimple::LessSimple ()
{
methods[0].name = "Name1";
methods[0].method = &LessSimple::method1;
methods[1].name = "Name2";
methods[1].method = &LessSimple::method2;
methods[2].name = "Name3";
methods[2].method = &LessSimple::method3;
}

/*------------------------------------------------------------------------
* LessSimple::doFunc
*------------------------------------------------------------------------*/

int
LessSimple::doFunc (const char *f, int val)
{
int i;


for (i = 0; i < 3; ++i)
{
if (std::strcmp (f, methods[i].name) == 0)
{
return (*methods[i].method)(this, val);
}
}

return -1;
}

For whatever reason, I get an error in LessSimple::LessSimple() on the 3 lines that set the value for methods[N].method. Here's the error:

methodPtr.C: In constructor 'LessSimple::LessSimple()':
methodPtr.C:142:38: error: cannot convert 'int (LessSimple::*)(int)' to 'LessSimple::mthd {aka int (*)(int)}' in assignment

If I change the definition of 'mthd' to:

typedef int (*LessSimple::mthd)(int);

The compiler errors saying:

methodPtr.C:55:19: error: extra qualification 'LessSimple::' on member 'mthd' [-fpermissive]
methodPtr.C:55:40: error: typedef name may not be a nested-name-specifier

So, I'm lost.

I'm testing this on Fedora 17 using the standard g++ compiler.

Thanks for any light you might be able to shed.
A pointer-to-member-function is completely different from a pointer-to-function.
Try typedef int (LessSimple::*mthd)(int);
naraku9333:

Thanks, that was it!
Topic archived. No new replies allowed.