我試圖插入與一個std ::設定值的unordered_map宣佈爲:unordered_map插入錯誤C++
class Database {
...
private:
struct CountryRCID {
int RCID;
int Vote;
};
struct comp {
bool operator() (const CountryRCID& left, const CountryRCID& right) const {
return left.RCID < right.RCID;
}
};
std::unordered_map<const char*, std::set<CountryRCID, comp> > CNTVotes;
};
在數據庫構造函數中,我從一個文件中讀取數據,並試圖插入在unordered_map
Database() {
char CNT[3];
CountryRCID RCIDVote;
... Insert data into CNT and RCIDVote ...
CNTVotes.insert(std::make_pair(CNT, RCIDVote));
}
我試圖編譯具有兩個密碼:
g++ main.cpp -std=gnu++0x
和
g++ main.cpp -std=c++0x
,但我收到的錯誤:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
from /usr/include/c++/4.4/bits/char_traits.h:41,
from /usr/include/c++/4.4/ios:41,
from /usr/include/c++/4.4/istream:40,
from /usr/include/c++/4.4/fstream:40,
from db.h:1,
from main.cpp:1:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U1 = char*, _U2 = Database::CountryRCID, _T1 = const char* const, _T2 = std::set<Database::CountryRCID, Database::comp, std::allocator<Database::CountryRCID> >]’:
db.h:50: instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:107: error: no matching function for call to ‘std::set<Database::CountryRCID, Database::comp, std::allocator<Database::CountryRCID> >::set(Database::CountryRCID)’
/usr/include/c++/4.4/bits/stl_set.h:212: note: candidates are: std::set<_Key, _Compare, _Alloc>::set(std::initializer_list<_CharT>, const _Compare&, const _Alloc&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:199: note: std::set<_Key, _Compare, _Alloc>::set(std::set<_Key, _Compare, _Alloc>&&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:188: note: std::set<_Key, _Compare, _Alloc>::set(const std::set<_Key, _Compare, _Alloc>&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:145: note: std::set<_Key, _Compare, _Alloc>::set(const _Compare&, const _Alloc&) [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
/usr/include/c++/4.4/bits/stl_set.h:136: note: std::set<_Key, _Compare, _Alloc>::set() [with _Key = Database::CountryRCID, _Compare = Database::comp, _Alloc = std::allocator<Database::CountryRCID>]
我也曾嘗試不同的插入方法:
CNTVotes[CNT] = RCIDVote;
和
std::pair <const char*, CountryRCID> test (CNT, RCIDVote);
CNTVotes.insert(test);
這只是導致類似錯誤
如果有人能幫助我理解它爲什麼不起作用以及我能做些什麼來完成這項工作,我將非常感激。
謝謝。
比較是關鍵,而不是值。另外你插入一個單一的對象而不是一個集合。但即使你解決了這個問題,它會崩潰,因爲你使用char指針作爲鍵的方式被搞砸了。 – 2011-05-17 18:57:32