2

I am trying to make a stl map with key as "KEYWORD" and value as "Class member function" But it is not getting compiled. Following is the code. Can anybody please let me know what is wrong. The class member functions are not static.

typedef void (RemoteHostManager::*CmdHandlerPtr)(char *);
typedef std::map<char *,CmdHandlerPtr> CommandHandlerSet;
typedef std::map<char *,CmdHandlerPtr>::iterator CommandHandlerSetItr;

void RemoteHostManager::InitializeCmdHandlerMap()
{
    m_CommandSet["HELP"]    = &RemoteHostManager::usage;
    m_CommandSet["CONNECT"] = &RemoteHostManager::Connect;
    m_CommandSet["READ"]    = &RemoteHostManager::Read;
    m_CommandSet["WRITE"]   = &RemoteHostManager::Write;
    m_CommandSet["STOP"]    = &RemoteHostManager::Stop;
    m_CommandSet["START"]   = &RemoteHostManager::Start;
}

Following are the errors:

RemoteHostManager.cpp: In member function `void
   RemoteHostManager::InitializeCmdHandlerMap()':
RemoteHostManager.cpp:14: no match for `std::_Rb_tree_iterator<std::pair<const
   std::string, void (RemoteHostManager::*)(char*)>, std::pair<const
   std::string, void (RemoteHostManager::*)(char*)>&, std::pair<const
   std::string, void (RemoteHostManager::*)(char*)>*>& [const char[5]]'
   operator
//similar error for other assignments!
6
  • the problem is it is getting compiler or it is NOT getting compiled? Commented Jan 18, 2011 at 6:44
  • @Avinash : what is the type of m_CommandSet?.. and tell us line number corresponds to which piece of code.. by the way, have you changed char* to std::string in the map? Commented Jan 18, 2011 at 7:18
  • So, what's m_CommandSet? You posted a bunch of typedef's, yet the most important part - the declaration of m_CommandSet - is missing. Commented Jan 18, 2011 at 7:19
  • @Avinash : I guess, the type of m_CommandSet is not CommandHandlerSet; it's rather CommandHandlerSetItr.Am I right? Commented Jan 18, 2011 at 7:22
  • @Nawaz and @AndreyT, you got it right, my m_commandSet was wrong. Commented Jan 18, 2011 at 7:23

2 Answers 2

3
typedef std::map<char *,CmdHandlerPtr> CommandHandlerSet;
typedef std::map<char *,CmdHandlerPtr>::iterator CommandHandlerSetItr;

First make it const char*, or even better std::string:

typedef std::map<std::string,CmdHandlerPtr> CommandHandlerSet;
typedef std::map<std::string,CmdHandlerPtr>::iterator CommandHandlerSetItr;

Note all your member functions should match the type of CmdHandlerPtr. That is, the parameter type must be char* , and return type must be void.

When using the map, you need an instance of type RemoteHostManager:

RemoteHostManager instance;
string key;
//...
(instance.*m_CommandSet[key])(param); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, But I am getting error at line m_CommandSet["HELP"] = &RemoteHostManager::usage; and usage memeber function is defined as void RemoteHostManager::usage( char *inCmd)
1

If class member functions are not static, you need to bind the method with an instance when setting it in the map. You could use boost::bind to do so.

2 Comments

What? Not with the syntax he's using. Also that would change the program from what he wants to do. Please read up on pointer-to-member-function variables.
On which instance will it call the method then if the methods aren't static ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.