Following on from my last question I have an abstract base class Action which acts as an interface for executing various different actions. In order to implement an abstraction layer, I have an ActionHandler class that stores various actions in this:
class ActionHandler { public: ActionHandler(); ~ActionHandler(); Action& getAction(std::string ActionString); private: boost::ptr_map<std::string, vx::modero::Action> cmdmap; }; I'm given to understand from the responses to my previous question that boost automatically handles freeing of any inserted pointer types (classes) into this map.
So, I now try inserting things derived from Action, this happens in the constructor of ActionHandler (ActionHandler::ActionHandler):
ActionHandler::ActionHandler() { this->cmdmap.insert("help", new DisplayHelpAction()); }; Where DisplayHelpAction publicly subclasses Action. Doing so causes this error:
error: no matching function for call to ‘boost::ptr_map<std::basic_string<char>, Action>::insert(const char [5], DisplayHelpAction*)’ Now, from here the method I'm using is:
std::pair<iterator,bool> insert( key_type& k, T* x ); So as far as I can see, using polymorphism here should work. I don't want to use boost::any because I don't want this list to contain any type of pointer. It should conform to the interface specified by Action or not be there.
So, what am I doing wrong?
I can fall back on simply using std::map and have my destructor delete, so if this can't be reasonably achieved it isn't a show stopper. I personally think shared_ptr with std::map might be better, but having experimented with this, I now have so-why-doesn't-this-work syndrome.
this->...call located in a non-const member function? There is too little context here. Please fill in the gaps.