469,576 Members | 1,682 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

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;
}
}

Feb 25 '07 #1
7 5995
DevNull wrote:
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;
}
}
It's simple enough, assignments are not allowed at global scope. You
must put all assignments in a function.

int main()
{
FunctionList["print"] = &print;
}

should compile.

john
Feb 25 '07 #2
On Feb 25, 4:28 am, John Harrison <john_androni...@hotmail.comwrote:
DevNull wrote:
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;
}
}

It's simple enough, assignments are not allowed at global scope. You
must put all assignments in a function.

int main()
{
FunctionList["print"] = &print;

}

should compile.

john
Wow thank you that did the trick!
Something like that you'ld think the compile would have a specific
message for.

Feb 25 '07 #3
On Feb 25, 1:28 pm, John Harrison
<john_androni...@hotmail.comwrote:
DevNull wrote:
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.
FunctionList["print"] = &print;

It's simple enough, assignments are not allowed at global
scope. You must put all assignments in a function.

int main()
{
FunctionList["print"] = &print;
}
There seem to be a number of other problems with the code.
typedef int(*Func)(const State&);
typedef std::map<std::string, Func*FL;
Are you sure you want to store pointers to pointers?
int print(State& state){
int print ( const State & state )
for(int count = 0; count < state.argc(); count++){
std::cout << state.args[count];
}
return 0;
}
FunctionList["print"] = &print;
Once you fix constness, & print will be a Func, but you're
trying to store Func * in your map, so this won't work
anyway.

--
roy axenov

Feb 25 '07 #4
On Feb 25, 4:45 am, "roy axenov" <r_axe...@mail.ruwrote:
On Feb 25, 1:28 pm, John Harrison

<john_androni...@hotmail.comwrote:
DevNull wrote:
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.
FunctionList["print"] = &print;
It's simple enough, assignments are not allowed at global
scope. You must put all assignments in a function.
int main()
{
FunctionList["print"] = &print;
}

There seem to be a number of other problems with the code.
typedef int(*Func)(const State&);
typedef std::map<std::string, Func*FL;

Are you sure you want to store pointers to pointers?
int print(State& state){

int print ( const State & state )
for(int count = 0; count < state.argc(); count++){
std::cout << state.args[count];
}
return 0;
}
FunctionList["print"] = &print;

Once you fix constness, & print will be a Func, but you're
trying to store Func * in your map, so this won't work
anyway.

--
roy axenov
Thank you, I have fixed those, they were typos.

Feb 25 '07 #5

"DevNull" <sm*****@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
On Feb 25, 4:45 am, "roy axenov" <r_axe...@mail.ruwrote:
>On Feb 25, 1:28 pm, John Harrison
[SNIP]
Thank you, I have fixed those, they were typos.
Just as a general remark: you might consider making your function table a
static map, as you most probably need only one unique instance, and wrap in
a class which also takes take of initializing, function lookup etc. IMO this
would give you cleaner and more flexible design, but naturally that's a
personal opinion.

Cheers
Chris

Feb 25 '07 #6
On Feb 25, 7:43 am, "Chris Theis" <chris.theis@n o s p am.cern.ch>
wrote:
"DevNull" <smor...@gmail.comwrote in message

news:11**********************@v33g2000cwv.googlegr oups.com...
On Feb 25, 4:45 am, "roy axenov" <r_axe...@mail.ruwrote:
On Feb 25, 1:28 pm, John Harrison
[SNIP]
Thank you, I have fixed those, they were typos.

Just as a general remark: you might consider making your function table a
static map, as you most probably need only one unique instance, and wrap in
a class which also takes take of initializing, function lookup etc. IMO this
would give you cleaner and more flexible design, but naturally that's a
personal opinion.

Cheers
Chris
Done and thank you for the feedback!

Feb 26 '07 #7
On Feb 26, 12:37 pm, "DevNull" <smor...@gmail.comwrote:
On Feb 25, 7:43 am, "Chris Theis" <chris.theis@n o s p am.cern.ch>
wrote:
Just as a general remark: you might consider making your function table a
static map, as you most probably need only one unique instance, and wrap in
a class which also takes take of initializing, function lookup etc. IMO this
would give you cleaner and more flexible design, but naturally that's a
personal opinion.
Cheers
Chris

Done and thank you for the feedback!
You should also take a look at using functor like objects. They may be
easier to use and initialise. If you have a functor super-class that
places itself into the map on initialisation (and removes itself on
destruction) then your maintenance will be easier because you just
declare a single const object for each functor type.

Simplified pseudo-code something like this:

class Functor;
map< string, const Functor * g_functors;

class Functor {
protected:
Functor( string name ) {
g_functors[ name ] = this;
}
virtual ~Functor() {
}
public:
virtual void execute( State * ) = 0;
};
Now for each one you just do this:

const class Print : class Functor {
public:
Print() : Functor( "print" ) {}
void execute( State * ) const {
// Whatever it does
}
} c_print;

This saves you from adding a function and remembering to add it to the
map as that is done automatically.

It also makes it easy to extend as you can just dynamically load
additional code at the time of executions (i.e. on Windows use
LoadLibrary to fetch a new DLL, which can even be specified on the
command line - I'm sure there must be something similar on UNIX
variants) and all of the new functions in the new code will
automatically become available.

This solution is more decoupled that storing raw function pointers. It
also allows for the objects to be initialised in different ways to
configure closely related functions. I.e.:

const class Print : class Functor {
public:
Print( ostream &stream, string name ) : Functor( name ),
m_stream( ostream ) {}
void execute( State * ) const {
// Whatever it does
}
private:
std::ostream &m_ostream;
} c_print( std::out, "print" ), c_log( g_mylogstream, "log" );
K

Feb 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Woodster | last post: by
25 posts views Thread by Christopher Benson-Manica | last post: by
44 posts views Thread by jmoy | last post: by
14 posts views Thread by Flzw | last post: by
2 posts views Thread by Serengeti | last post: by
13 posts views Thread by kamaraj80 | last post: by
7 posts views Thread by Matthias Pfeifer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.