How to call a class member function via function pointer in map [C++]

Jul 6, 2009 at 5:44am
I'm trying to declare a std::map that is keyed by a char and has a function pointer as a second parameter, these functions would be a member function of my class. But for some odd reason I cannot call the functions when extracted...

So, I have a class A as shown below, it contains a map as well as a function

[Code]
class A
{
private:
map<char, void (A::*)(void)> mapA;
public:
void func();

[Code]

Next I have main code that looks like this:
[Code]
A::A() // constructor
{
// Generate User Options
mapA['a'] = &A::func;
}
[Code]

Now, I try to execute func by extracting it from the map and running the function:
[Code]
map<char, void (A::*)(void)>::iterator it;
for ( it=mapA.begin() ; it != mapA.end(); it++ )
{
(*it).second();
}
[Code]

I would assume this would launch function "func" as it is stored in the map, but instead it generates the following error message:
error C2064: term does not evaluate to a function taking 0 arguments

Any help would be greatly appreciated...
Thanks,
Last edited on Jul 6, 2009 at 5:44am
Jul 6, 2009 at 10:07am
Change this:
1
2
3
4
5
map<char, void (A::*)(void)>::iterator it;
for ( it=mapA.begin() ; it != mapA.end(); it++ )
{
    (*it).second();
}


to:
1
2
3
4
5
map<char, void (A::*)(void)>::iterator it;
for ( it=mapA.begin() ; it != mapA.end(); it++ )
{
    (this->* (*it).second)();
}

Jul 6, 2009 at 2:56pm
[code]
This is how you use code tags.
[/code]

------------

[Code]
Not like this.
[Code]
Last edited on Jul 6, 2009 at 2:57pm
Jul 6, 2009 at 3:44pm
or just mark the code and klick the "#"-button to the right;)...
Topic archived. No new replies allowed.