Weird function pointer problem...

Ok, I have a function pointer defined as such:

typedef void (animate::*animate_func_ptr)(std::string);

I have a vector of a pair with some of these defined as such:

std::vector< std::pair<std::string, animate_func_ptr> > global_commands;

Adding commands seems to work, but when I attempt to use them I get problems...here is the function to do the commands:

1
2
3
4
5
6
7
8
9
void animate::do_command(std::string command, std::string argument) {
	command = strtolower(command);
	argument = strtolower(argument);
	for(size_t i = 0; i < mud_info->global_commands.size(); ++i) {
		if(mud_info->global_commands.at(i).first.find(command) == 0) { //if we see part of the command
			(*(mud_info->global_commands.at(i).second))(argument); //do the command we find
		}
	}
}


This gives me two errors:
Error 28 error C2064: term does not evaluate to a function taking 1 arguments
Error 27 error C2171: '*' : illegal on operands of type 'animate_func_ptr '

The problem is the second one mainly, I can't figure out why it is complaining that I can't deference a function pointer...am I screwing something up? If you need more info just ask.
I'm assuming mud_info is your 'animate' object on which you want to call the function pointer. If that's the case you need to supply it as the object:

line 6
 
(mud_info->*(mudinfo->global_commands.at(i).second))(argument);
Oh, sorry, mud_info is a class where the global commands are (public of course):

1
2
3
4
5
6
class mud_info {
public:
//...
    std::vector< std::pair<std::string, animate_func_ptr> > global_commands;
//...
}
well the problem here is that animate_func_ptr is a pointer to an animate member function. So in order to call it you need an animate object. If mud_info wasn't it, then I don't see one here.

The syntax for that is:

1
2
3
4
animate* obj = whatever;
animate_func_ptr func = whatever;

(obj->*func)( somestring );


If this is a member function, you need to supply that object.
Ah...it worked like a charm! I guess I should have wondered how the function pointer would get the this parameter without me telling it. Thanks :D
I know I'm late coming to this thread, but here's a plug for
The Function Pointer Tutorials
http://www.newty.de/fpt/index.html
Funnily enough, that is what I was looking at for a reference.
Topic archived. No new replies allowed.