c++ - Nested unordered_map has no member named ‘emplace’ -
i trying fix pretty hard program me got gamedev book. think it's crashed because author used windows , use linux (g++). in short have couple of classes perform application state's logic, , have map of maps hold states callbacks:
enum class statetype { intro = 1, mainmenu, game, paused, gameover, credits }; using bindings = std::unordered_map<std::string, binding*>; using callbackcontainer = std::unordered_map<std::string, std::function<void(eventdetails*)>>; using callbacks = std::unordered_map<statetype, callbackcontainer>; class eventmanager { public: ... template<class t> bool addcallback(statetype l_state, const std::string& l_name, void(t::*l_func)(eventdetails*), t* l_instance) { auto itr = m_callbacks.emplace(l_state, callbackcontainer()).first; auto temp = std::bind(l_func, l_instance, std::placeholders::_1); return itr->second.emplace(l_name, temp).second; } private: callbacks m_callbacks;
i'm not sure parts of code include here. anyway terrible stack trace:
/usr/include/c++/5/bits/hashtable_policy.h: in instantiation of ‘struct std::__detail::__is_noexcept_hash<statetype, std::hash<statetype> >’: /usr/include/c++/5/type_traits:137:12: required ‘struct std::__and_<std::__is_fast_hash<std::hash<statetype> >, std::__detail::__is_noexcept_hash<statetype, std::hash<statetype> > >’ /usr/include/c++/5/type_traits:148:38: required ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<statetype> >, std::__detail::__is_noexcept_hash<statetype, std::hash<statetype> > > >’ /usr/include/c++/5/bits/unordered_map.h:100:66: required ‘class std::unordered_map<statetype, std::function<basestate*()> >’ /home/xxx/projects/mushrooom/basestate.h:48:28: required here /usr/include/c++/5/bits/hashtable_policy.h:85:34: error: no match call ‘(const std::hash<statetype>) (const statetype&)’ noexcept(declval<const _hash&>()(declval<const _key&>()))> ... /usr/include/c++/5/bits/unordered_map.h:649:7: error: ‘value’ not member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<statetype> >, std::__detail::__is_noexcept_hash<statetype, std::hash<statetype> > > >’ equal_range(const key_type& __x) const ... /home/xxx/projects/mushrooom/eventmanager.h: in member function ‘bool eventmanager::addcallback(statetype, const string&, void (t::*)(eventdetails*), t*)’: /home/xxx/projects/mushrooom/eventmanager.h:93:32: error: ‘using callbacks = class std::unordered_map<statetype, std::unordered_map<std::basic_string<char>, std::function<void(eventdetails*)> > > {aka class std::unordered_map<statetype, std::unordered_map<std::basic_string<char>, std::function<void(eventdetails*)> > >}’ has no member named ‘emplace’ auto itr = m_callbacks.emplace(l_state, callbackcontainer()).first;
seems callbacks has no member emplace, it's std::unordered_map , has such method.
g++-5, linux
it has nothing emplace - missing hash function!
you using std::unordered_map, in other words hash map. if want use object key, object must provide function hash value can calculated.
you have 2 options:
- provide template specialisation of std::hash class or pass own class third (hasher) template parameter std::unordered_map
- use std::map instead - tree-map not requiring hash function.
Comments
Post a Comment