Cannot use map in factory pattern using c++ -
i trying create basic type wrapper project , want use factory create types depending on user input.
to illustrate code boolean type
class typeboolean :public generictypewrapper<bool>, public itypewrapper { public: typeboolean(bool b = false) { this->setvalue(b); } std::string gettypename() { return "boolean"; } static itypewrapper* __stdcall create(){ return new typeboolean(); } };
the generictypewrapper class getters , the setters , itypewrapper class abstract class gettypename function in it
now problem factory
using createtypefunction = std::function<itypewrapper*(void)>; class typefactory { private: static std::map<std::string, createtypefunction> creationfunctions; typefactory() { std::vector<std::string> listoftypenames = { "boolean" }; std::vector<createtypefunction> listofcreatefunctions = { typeboolean::create() }; (unsigned int = 0; < listoftypenames.size(); i++) { creationfunctions.insert(listoftypenames[i], listofcreatefunctions[i]); } } };
now there more types problem reproduces 1 type well. error on following line
creationfunctions.insert(listoftypenames[i], listofcreatefunctions[i]);
the error states :
error c2664: 'void std::_tree<std::_tmap_traits<_kty,_ty,_pr,_alloc,false>>::insert(std::initializer_list<std::pair<const _kty,_ty>>)' : cannot convert argument 1 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' 'std::_tree_const_iterator<std::_tree_val<std::_tree_simple_types<std::pair<const _kty,_ty>>>>'
i not understand why error thrown, suggestion welcome.
the method map::insert
not take key , value, iterator , value_type.
try line instead:
creationfunctions[listoftypenames[i]] = listofcreatefunctions[i];
are sure line compiles without warnings:
std::vector<createtypefunction> listofcreatefunctions = { typeboolean::create() };
looks fishy me, have expected &typeboolean::create
maybe it's new c++11 initializer feature missed.
Comments
Post a Comment