错误:没有用于调用' std :: map的匹配函数

时间:2015-07-10 14:10:20

标签: c++ dictionary stl

typedef struct {
    string strDatabaseName;
    set <string, greater<string> > setDBAccName;
} UserDBAInfo_t;

typedef struct {
    map<int, UserDBAInfo_t > mapUserDBAInfo;
} UserDBInfo_t;

typedef set<string, greater<string> > setNames_t;

int main( int argc, char * argv[] )
{
    ...
    map<string, UserDBInfo_t > mapHRUserDBInfo;

    UserDBInfo_t structUserDBInfo;
    UserDBAInfo_t structUserDBAInfo;

    structUserDBAInfo.strDatabaseName = strDatabaseName;
    structUserDBAInfo.setDBAccName.insert(strDBAccName);

    structUserDBInfo.mapUserDBAInfo.insert(nDatabaseID, structUserDBAInfo);

    mapHRUserDBInfo.insert(make_pair(strSabun, structUserDBInfo));   <--- compile error here

    ...

}

当我编译它时,我收到了错误消息。

  

main.cpp:2778:错误:没有匹配函数来调用&#39; std :: map&lt; int,UserDBAInfo_t,std :: less&lt; int&gt ;,std :: allocator&lt; std :: pair&lt; const int ,UserDBAInfo_t&gt; &GT; &gt; :: insert(int&amp;,UserDBAInfo_t&amp;)&#39;   /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:395:注意:候选人是: std :: pair&lt; typename std :: _ Rb_tree&lt; _Key,std :: pair&lt; const _Key,_Tp&gt;,std :: _ Select1st&lt; std :: pair&lt; const _Key,_Tp&gt; &gt;,_比较,typename _Alloc :: rebind&lt; std :: pair&lt; const _Key,_Tp&gt; &gt; :: other&gt; :: iterator,bool&gt; std :: map&lt; _Key,_Tp,_Compare,_ Alloc&gt; :: insert(const std :: pair&lt; const _Key,_Tp&gt;&amp;)[with _Key = int,_Tp = UserDBAInfo_t,_Compare = std :: less&lt; int&gt; ,_Alloc = std :: allocator&lt; std :: pair&lt; const int,UserDBAInfo_t&gt; &GT;]   /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:419:注意:typename std: :_Rb_tree&lt; _Key,std :: pair&lt; const _Key,_Tp&gt;,std :: _ Select1st&lt; std :: pair&lt; const _Key,_Tp&gt; &gt;,_比较,typename _Alloc :: rebind&lt; std :: pair&lt; const _Key,_Tp&gt; &gt; :: other&gt; :: iterator std :: map&lt; _Key,_Tp,_Compare,_ Alloc&gt; :: insert(typename std :: _ Rb_tree&lt; _Key,std :: pair&lt; const _Key,_Tp&gt;,std :: _ Select1st&lt; std :: pair&lt; const _Key,_Tp&gt;&gt;,_ Compare,typename _Alloc :: rebind&lt; std :: pair&lt; const _Key,_Tp&gt;&gt; :: other&gt; :: iterator,const std :: pair&lt; const _Key, _Tp&gt;&amp;)[with _Key = int,_Tp = UserDBAInfo_t,_Compare = std :: less&lt; int&gt;,_ Alloc = std :: allocator&lt; std :: pair&lt; const int,UserDBAInfo_t&gt; &GT;]

可能出现什么问题?

1 个答案:

答案 0 :(得分:2)

错误消息no matching function for call to 'std::map, std::allocator > >::insert(int&, UserDBAInfo_t&)向我表明问题出在行中:

structUserDBInfo.mapUserDBAInfo.insert(nDatabaseID, structUserDBAInfo);

不是你在问题中提到的那一行。那应该是:

structUserDBInfo.mapUserDBAInfo.insert(make_pair(nDatabaseID, structUserDBAInfo));

如果您能够使用C ++ 11编译器,您还可以使用:

structUserDBInfo.mapUserDBAInfo.emplace(nDatabaseID, structUserDBAInfo);