unordered_map插入错误C ++
|
我正在尝试将具有这样声明的std :: set值的unordered_map插入:
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);
只会导致类似的错误
如果有人能帮助我了解它为什么不起作用以及我可以做些什么来使它起作用,我将非常感激。
谢谢。
没有找到相关结果
已邀请:
4 个回复
长拳
是
类型,而不是
...您首先需要构造
类型的
并将其用于
的自变量中,以匹配
的模板自变量。 我还认为
函子是多余的...只需为
结构定义
和
。 如果这样做,请将and18ѭ的声明更改为(BTW,请注意,我借用尼尔的建议是使用
而不是
作为键值...我认为这非常好理念)
那么您可以执行以下操作:
死搭胯
您可能想要:
瞧叮
中。 编辑:实际上,由于您有一个实际的对象,我认为您可以像这样使用
构造函数:
久纳禾澎
结构。如果您打算将ѭ7添加到地图中的集合中,则可能需要以下内容: