Thread: Making a map containing function pointers

  1. #1
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34

    Making a map containing function pointers

    Im trying to make a DOS like program (you type in a command and some arguments and it runs a function based on your input). I want to store these functions in a map, however, when I try and compile the compiler (MSVC++ 6.0) gives me some very long complicated errors that I can't make head or tail of.

    Like this:

    c:\program files\microsoft visual studio\vc98\include\functional(86) : error C2784: 'bool __cdecl std::operator <(const class std::multimap<_K,_Ty,_Pr,_A> &,const class std::multimap<_K,_Ty,_Pr,_A> &)' : could not deduce template argument for 'const
    class std::multimap<_K,_Ty,_Pr,_A> &' from 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
    c:\program files\microsoft visual studio\vc98\include\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::opera
    tor ()(const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &) const'

    (Also, they direct me to somewhere in one of the include files, so I don't want to start changing things there).

    Basically I'm trying to use a:

    map<string, void (*)(vector<string>&)>

    I'm beginning to wonder if this is possible, it would kind of blow my whole experiment to hell if it isn't though. Anyway, any help would be great.

  2. #2
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Excellent.

    *goes off to search for other errors*

    EDIT: That code works, but gives me 110 warnings.
    Last edited by Nippashish; 12-21-2002 at 09:52 PM.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You need this.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    That's a great app Stoned Coder, now I can actually read my errors.

    Unfortianately I'm still stuck. I started over, and right now I have the most basic possible version of what I'm trying to do, and it's still giving me errors I don't understand. (I can read them now, I just can't figure out why I'm getting them).

    Here's my code I can't get to compile:
    Code:
    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <map>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;
    using std::map;
    
    typedef vector<string> argVec;
    typedef map<string, void (*)(argVec&)> CommandMap;
    
    void buildCommandMap(CommandMap&);
    void Command_test(argVec&);
    
    int main()
    {
    	CommandMap cmdMap;	// map of all accepted commands;
    	buildCommandMap(cmdMap);	// put all the commands into the map
    	argVec x;
    	cmdMap["test"](x);
    	return 0;
    }
    
    void buildCommandMap(CommandMap &cmdMap)
    {
    	cmdMap["test"] = Command_test;
    }
    
    void Command_test(argVec &argv)
    {
    	cout << "Command_test called!" << endl;
    }

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    1
    Stoned Coder: Thanks for the STLFilt plug!

    Nippashish: this was a good one... I spent an hour messing around, and getting very confused as to why this code compiles under Comeau and Metrowerks CodeWarrior, but not under any version of MSVC (including the 7.1 beta).

    Finally, getting desparate, I checked the header files. You do not explicitly include <string> (<cstring> is basically <string.h>, not the same thing). When I add the header, it works with all version of MSVC (although it does give a stupid truncation message in MSVC6).

    I haven't tried to figure out where the identifier "string" is intruding to mess things up when you neglect to include the header. I'm not sure I want to know ;-)
    -leor

  6. #6
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Thanks, guys. I've got it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM