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

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

Next I have main code that looks like this:

A::A()	// constructor
{
	// Generate User Options
	mapA['a'] = &A::func;
}

Now, I try to execute func by extracting it from the map and running the function:

map<char, void (A::*)(void)>::iterator it;
	for ( it=mapA.begin() ; it != mapA.end(); it++ )
	{
		(*it).second();
	}

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,

Recommended Answers

All 4 Replies

It works with static method only.

try the following.

the syntax seems difficult but look it carefully.

(f.*((*it).second))();
(f.*(it->second))();

the easier way is to create the typedef and assign the return value of m to that, and then call that pointer to member function.

like below.

typedef void (A::*FMemPtr)(void);

map<char, FMemPtr> m;
m['a'] = &A::func;

FMemPtr funcPtr = m['a'];
A obj;
(obj.*funcPtr)();

same goes for iterator.

hope this helps

by the way following is also a legal syntax.

(obj.*m['a'])();

:).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.