Thread: help with boost function in std map

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    help with boost function in std map

    Hello

    Why will the following code produce error:

    error C2664: 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::insert(std::_Tree<_Traits>::i terator,const std:air<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'std::string' to 'std::_Tree<_Traits>::iterator'
    1> with
    1> [
    1> _Traits=std::_Tmap_traits<std::string,user_input:: command_handler,std::less<std::string>,std::alloca tor<std:air<const std::string,user_input::command_handler>>,false>,
    1> _Ty1=const std::string,
    1> _Ty2=user_input::command_handler
    1> ]
    1> and
    1> [
    1> _Traits=std::_Tmap_traits<std::string,user_input:: command_handler,std::less<std::string>,std::alloca tor<std:air<const std::string,user_input::command_handler>>,false>
    1> ]
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    Code:
    class user_input {
    public:
    	user_input() {
    
    	}
    
    	class command_handler {
    	public:
    		command_handler() { }
    		command_handler(
    			boost::program_options::options_description desc,
    			boost::function<void (boost::program_options::variables_map vars)> f) :
    				m_desc(desc), m_func(f) {
    				}
    
    
    	private:
    		boost::program_options::options_description m_desc;
    		boost::function<void (variables_map vars)> m_func;
    	};
    
    
    	void add_function(std::string name, boost::program_options::options_description desc,
    		boost::function<void (boost::program_options::variables_map vars)> func)
    	{
    		m_handlers.insert(name, command_handler(desc, func) );
    	}
    
    private:
    	std::map<std::string, command_handler> m_handlers;
    };

    Many thanks for help!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The insert member function takes a single pair<string,command_handler> object as an argument, not separate/individual string/command_handler objects. Probably something like:
    Code:
    #include <utility>
    ...
    m_handlers.insert( std::make_pair(name,command_handler(desc,func)) );
    [edit]Or maybe you could have just done:
    Code:
    m_handlers[name] = command_handler(desc,func);
    [/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM