Problem with std::map of function pointers
Hi there everyone,
I'm creating a very simple immediate mode command interpreter.
The final purpose is to provide a pluggable control and command
console for a MUD server I have written.
The basic theory is we wrap the functions we want exposed to the
console in a function with a prototype of
int func(State*)
the State* is an object that contains both
args, and results as std::vector<std::string>
Next we tie the wrapped functions up with a
std::map<std::string,Func*>
So that we can call the functions by name.
The problem I'm presently having is coming from the compiler, and
reads...
console.h:39: error: expected constructor, destructor, or type
conversion before '=' token
Line 39 reads...
FunctionList["print"] = &print;
That should be perfectly legal since my relevant code looks like...
typedef int(*Func)(const State&);
typedef std::map<std::string, Func*> FL;
FL FunctionList;
int print(State& state){
for(int count = 0; count < state.argc(); count++){
std::cout << state.args[count];
}
return 0;
}
FunctionList["print"] = &print;
I have googled, and searched through years of newsgroup postings and
I'm not finding anything that might be causing this problem.
I'm hoping someone can help.
Below is a full code listing, it's only 56 lines including includes
and comments, so I'm hoping no one minds me posting it, my hope is
that someone can point out something I've missed.
Thanks in advance for the help!
#include <iostream>
#include <vector>
#include <map>
class State{
public:
std::vector<std::string>
args,//Argument List
result; //Final Result
~State(){
while(args.size() != 0){
args.pop_back();
}
while(result.size() != 0){
result.pop_back();
}
}
inline int argc(){return(args.size());}
};
typedef int(*Func)(const State&);
typedef std::map<std::string, Func*> FL;
typedef std::map<std::string, Func*>::value_type MapItem;
FL FunctionList;
int print(State& state){
for(int count = 0; count < state.argc(); count++){
std::cout << state.args[count];
}
return 0;
}
FunctionList["print"] = &print;
int execute(Func* func,std::vector<std::string>& args){
State state;
state.args.swap(args);
(*func)(state);
}
int parse(std::vector<std::string>& input){
FL::iterator iter = FunctionList.find(input.at(0));
if(iter != FunctionList.end()){
Func* func = iter->second;
execute(func,input);
}else{
std::cout << "Error: Command " << input.at(0) << " was not found!"
<< std::endl;
}
}