2014-09-02 21 views
0

什麼是wxWidgets的良好字典類型? 框架中是否有這樣的類型? 我應該堅持數組嗎?我試過unordered_map,但給我錯誤的wxString類型。wxwidgets的良好字典類型

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

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

錯誤:

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; 
+0

究竟是什麼錯誤?嘗試'wxT(「first」)' - 只是猜測 – 2014-09-02 11:13:09

+0

即使當我不初始化我得到這些錯誤 – aelgoa 2014-09-02 11:18:26

回答

0

總是有wxHashMap

WX_DECLARE_STRING_HASH_MAP(int,  // type of the values 
          myMapType); // name of the class 

myMapType myMap; 
myMap["test"]=321; 

要在unordered_map使用wxString,你需要爲wxString提供的哈希函數。使用std :: wstring可能更簡單。

所以你的選擇:

  1. 使用wxHashMap這立即提供wxString支持,但不是對所有最新的C + 11個善良。
  2. 將unordered_map與家庭釀造的wxString散列函數結合使用。
  3. 將unordered_map與std :: wstring一起使用。你將不得不wxString和std :: wstring的相互轉換,但將獲得wxWidgets的V3.1.0的C++ 11
  4. 等待完整的支持

我建議選擇3號,現在

+0

偉大的,我錯過了可能性有內聯語法雖然初始化。 – aelgoa 2014-09-02 13:31:15

0

由於這個問題,wxWidgets 3.1.0將擁有std::hash<wxString>專業化(請參閱this commit,因此您的示例應該可以開箱即用。)

+0

確實快速工作! – ravenspoint 2014-09-02 15:43:22