Search code examples
dictionarywxwidgets

good dictionary type for wxwidgets


What would be a good dictionary type to use with wxWidgets? Is there such a type somewhere in the framework? Should I just stick with arrays? I've tried unordered_map but giving me errors with the wxString type.

#include <unordered_map>
std::unordered_map<wxString,int>mapsi;

mapsi={
  {"first",1},
  {"second",2},
};

errors:

In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
                 from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h: In ins
tantiation of 'struct std::__detail::_Hash_code_base<wxString, std::pair<const w
xString, int>, std::__detail::_Select1st, std::hash<wxString>, std::__detail::_M
od_range_hashing, std::__detail::_Default_ranged_hash, true>':
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1402:10
:   required from 'struct std::__detail::_Hashtable_base<wxString, std::pair<con
st wxString, int>, std::__detail::_Select1st, std::equal_to<wxString>, std::hash
<wxString>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_ha
sh, std::__detail::_Hashtable_traits<true, false, true> >'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:174:11:   requ
ired from 'class std::_Hashtable<wxString, std::pair<const wxString, int>, std::
allocator<std::pair<const wxString, int> >, std::__detail::_Select1st, std::equa
l_to<wxString>, std::hash<wxString>, std::__detail::_Mod_range_hashing, std::__d
etail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail:
:_Hashtable_traits<true, false, true> >'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h:100:18:
required from 'class std::unordered_map<wxString, int>'
E:\bootsi\New folder\TEST.cpp:96:34:   required from here
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1070:12
: error: invalid use of incomplete type 'struct std::hash<wxString>'
     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
            ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
     struct hash;
            ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
                 from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1070:12
: error: invalid use of incomplete type 'struct std::hash<wxString>'
     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
            ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
     struct hash;
            ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
                 from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1082:53
: error: invalid use of incomplete type 'struct std::hash<wxString>'
       using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
       In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
     struct hash;
            ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
                 from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1082:53
: error: invalid use of incomplete type 'struct std::hash<wxString>'
       using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
                                                     ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
     struct hash;
            ^
E:\bootsi\New folder\TEST.cpp: In constructor 'std::unordered_map<_Key,
 _Tp, _Hash, _Pred, _Alloc>::unordered_map(std::unordered_map<_Key, _Tp, _Hash,
_Pred, _Alloc>::size_type, const hasher&, const key_equal&, const allocator_type
&) [with _Key = wxString; _Tp = int; _Hash = std::hash<wxString>; _Pred = std::e
qual_to<wxString>; _Alloc = std::allocator<std::pair<const wxString, int> >; std
::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type = unsigned int; std:
:unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hasher = std::hash<wxString>; s
td::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::key_equal = std::equal_to<wx
String>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::allocator_type = s
td::allocator<std::pair<const wxString, int> >]':
E:\bootsi\New folder\TEST.cpp:96:34: error: invalid use of incomplete t
ype 'std::unordered_map<wxString, int>::hasher {aka struct std::hash<wxString>}'

  std::unordered_map<wxString,int>mapsi;
                                  ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'std::unordered_map<wxString, int>::hasher {aka struct std:
:hash<wxString>}'
     struct hash;

E:\bootsi\New folder\TEST.cpp:96:34: note:   when instantiating default
 argument for call to std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unord
ered_map(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const h
asher&, const key_equal&, const allocator_type&) [with _Key = wxString; _Tp = in
t; _Hash = std::hash<wxString>; _Pred = std::equal_to<wxString>; _Alloc = std::a
llocator<std::pair<const wxString, int> >; std::unordered_map<_Key, _Tp, _Hash,
_Pred, _Alloc>::size_type = unsigned int; std::unordered_map<_Key, _Tp, _Hash, _
Pred, _Alloc>::hasher = std::hash<wxString>; std::unordered_map<_Key, _Tp, _Hash
, _Pred, _Alloc>::key_equal = std::equal_to<wxString>; std::unordered_map<_Key,
_Tp, _Hash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const wxSt
ring, int> >]
  std::unordered_map<wxString,int>mapsi;
                                  ^
E:\bootsi\New folder\TEST.cpp: At global scope:
E:\bootsi\New folder\TEST.cpp:149:19: error: expected constructor, dest
ructor, or type conversion before ';' token
  CreateStatusBar();
                   ^
E:\bootsi\New folder\TEST.cpp:150:15: error: expected constructor, dest
ructor, or type conversion before '(' token
  SetStatusText("Welcome to wxWidgets!");
               ^
E:\bootsi\New folder\TEST.cpp:151:1: error: expected declaration before
 '}' token
 }
 ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
                 from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h: In ins
tantiation of 'std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H
2, std::__detail::_Default_ranged_hash, true>::_Hash_code_base(const _ExtractKey
&, const _H1&, const _H2&, const std::__detail::_Default_ranged_hash&) [with _Ke
y = wxString; _Value = std::pair<const wxString, int>; _ExtractKey = std::__deta
il::_Select1st; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_hashi
ng]':
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1463:65
:   required from 'std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Eq
ual, _H1, _H2, _Hash, _Traits>::_Hashtable_base(const _ExtractKey&, const _H1&,
const _H2&, const _Hash&, const _Equal&) [with _Key = wxString; _Value = std::pa
ir<const wxString, int>; _ExtractKey = std::__detail::_Select1st; _Equal = std::
equal_to<wxString>; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_h
ashing; _Hash = std::__detail::_Default_ranged_hash; _Traits = std::__detail::_H
ashtable_traits<true, false, true>]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:828:24:   requ
ired from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::_Hashtable(std::_Hashtable<_Key, _Value, _Alloc,
 _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type, const
 _H1&, const _H2&, const _Hash&, const _Equal&, const _ExtractKey&, const alloca
tor_type&) [with _Key = wxString; _Value = std::pair<const wxString, int>; _Allo
c = std::allocator<std::pair<const wxString, int> >; _ExtractKey = std::__detail
::_Select1st; _Equal = std::equal_to<wxString>; _H1 = std::hash<wxString>; _H2 =
 std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash;
 _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_
Hashtable_traits<true, false, true>; std::_Hashtable<_Key, _Value, _Alloc, _Extr
actKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type = unsigned i
nt; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
_RehashPolicy, _Traits>::allocator_type = std::allocator<std::pair<const wxStrin
g, int> >]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable.h:397:26:   requ
ired from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2,
_Hash, _RehashPolicy, _Traits>::_Hashtable(std::_Hashtable<_Key, _Value, _Alloc,
 _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::size_type, const
 _H1&, const key_equal&, const allocator_type&) [with _Key = wxString; _Value =
std::pair<const wxString, int>; _Alloc = std::allocator<std::pair<const wxString
, int> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<wxStri
ng>; _H1 = std::hash<wxString>; _H2 = std::__detail::_Mod_range_hashing; _Hash =
 std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_reha
sh_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::_
Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPol
icy, _Traits>::size_type = unsigned int; std::_Hashtable<_Key, _Value, _Alloc, _
ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::key_equal = std::e
qual_to<wxString>; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H
1, _H2, _Hash, _RehashPolicy, _Traits>::allocator_type = std::allocator<std::pai
r<const wxString, int> >]'
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\unordered_map.h:142:35:
required from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::unordered_ma
p(std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type, const hasher&,
 const key_equal&, const allocator_type&) [with _Key = wxString; _Tp = int; _Has
h = std::hash<wxString>; _Pred = std::equal_to<wxString>; _Alloc = std::allocato
r<std::pair<const wxString, int> >; std::unordered_map<_Key, _Tp, _Hash, _Pred,
_Alloc>::size_type = unsigned int; std::unordered_map<_Key, _Tp, _Hash, _Pred, _
Alloc>::hasher = std::hash<wxString>; std::unordered_map<_Key, _Tp, _Hash, _Pred
, _Alloc>::key_equal = std::equal_to<wxString>; std::unordered_map<_Key, _Tp, _H
ash, _Pred, _Alloc>::allocator_type = std::allocator<std::pair<const wxString, i
nt> >]'
E:\bootsi\New folder\TEST.cpp:96:34:   required from here
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1099:63
: error: invalid use of incomplete type 'struct std::hash<wxString>'
       : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
                                                               ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
     struct hash;
            ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hash
table.h:35:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\unordered
_map:47,
                 from E:\bootsi\New folder\TEST.cpp:9:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\hashtable_policy.h:1099:63
: error: invalid use of incomplete type 'struct std::hash<wxString>'
       : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
                                                               ^
In file included from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basi
c_string.h:3033:0,
                 from d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\string:52
,
                 from D:/build/wxWidgets-3.0.1/include/wx/stringimpl.h:66,
                 from D:/build/wxWidgets-3.0.1/include/wx/unichar.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/strvararg.h:22,
                 from D:/build/wxWidgets-3.0.1/include/wx/string.h:46,
                 from D:/build/wxWidgets-3.0.1/include/wx/memory.h:15,
                 from D:/build/wxWidgets-3.0.1/include/wx/object.h:19,
                 from D:/build/wxWidgets-3.0.1/include/wx/wx.h:15,
                 from E:\bootsi\New folder\TEST.cpp:5:
d:\build\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\functional_hash.h:58:12: e
rror: declaration of 'struct std::hash<wxString>'
     struct hash;

Solution

  • There is always wxHashMap

    WX_DECLARE_STRING_HASH_MAP( int,     // type of the values
                                myMapType); // name of the class
    
    myMapType myMap;
    myMap["test"]=321;
    

    To use wxString in a unordered_map, you will need to provide a hash function for wxString. It might be simpler to use std::wstring.

    So you have the choices:

    1. Use wxHashMap which provides support for wxString immediately, but not for all the latest C+11 goodness.
    2. Use unordered_map with a home brewed wxString hash function.
    3. Use unordered_map with std::wstring. You will have to convert between wxString and std::wstring, but will get complete support for C++11
    4. Wait of wxWidgets v3.1.0

    I recommend option #3, for now