C++ map使用operator[]报错:无匹配F1Team默认构造函数
问题描述
我定义了如下C++类:
class F1Team { private: string name; string racer1; string racer2; protected: int titles; public: F1Team(string tnm) { name = tnm; titles = 0; } void set_racer1(string r1) { racer1 = r1; } void set_racer2(string r2) { racer2 = r2; } void display() { cout<< name<< endl; } };
我想要存储int到F1Team对象的映射,编写了以下代码:
int main() { map<int, F1Team> mp; for (int i=0; i<3; ++i) { F1Team t {"Mercedes"}; mp.insert(pair<int , F1Team>(i, t)); } for (auto itr = mp.begin(); itr != mp.end(); ++itr) { (*itr).second.display(); } mp[1].display(); return 0; }
但调用mp[1].display()时触发了编译错误,错误信息如下:
In file included from /usr/include/c++/11/bits/stl_map.h:63, from /usr/include/c++/11/map:61, from test.cpp:3: /usr/include/c++/11/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {int&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = F1Team]’: /usr/include/c++/11/tuple:1813:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = F1Team]’: /usr/include/c++/11/ext/new_allocator.h:162:4: required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, F1Team>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, F1Team> >]’: /usr/include/c++/11/bits/alloc_traits.h:516:17: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, F1Team>; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, F1Team> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const int, F1Team> > >]’: /usr/include/c++/11/bits/stl_tree.h:595:32: required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, F1Team> >*]’: /usr/include/c++/11/bits/stl_tree.h:612:21: required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, F1Team> >*]’: /usr/include/c++/11/bits/stl_tree.h:2431:33: required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, F1Team>; _KeyOfValue = std::_Select1st<std::pair<const int, F1Team> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree<int, std::pair<const int, F1Team>, std::_Select1st<std::pair<const int, F1Team> >, std::less<int>, std::allocator<std::pair<const int, F1Team> > >::iterator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree<int, std::pair<const int, F1Team>, std::_Select1st<std::pair<const int, F1Team> >, std::less<int>, std::allocator<std::pair<const int, F1Team> > >::const_iterator]’: /usr/include/c++/11/bits/stl_map.h:520:37: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = F1Team; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, F1Team> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = F1Team; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’: test.cpp:45:9: required from here /usr/include/c++/11/tuple:1824:9: error: no matching function for call to ‘F1Team::F1Team()’ 1824 | second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test.cpp:17:9: note: candidate: ‘F1Team::F1Team(std::string)’ 17 | F1Team(string tnm) { | ^~~~~~ test.cpp:17:9: note: candidate expects 1 argument, 0 provided test.cpp:7:7: note: candidate: ‘F1Team::F1Team(const F1Team&)’ 7 | class F1Team { | ^~~~~~ test.cpp:7:7: note: candidate expects 1 argument, 0 provided test.cpp:7:7: note: candidate: ‘F1Team::F1Team(F1Team&&)’ test.cpp:7:7: note: candidate expects 1 argument, 0 provided
我无法理解为何调用mp[1].display()时会尝试调用F1Team的默认构造函数,进而导致编译错误。
问题原因与解决方法
核心原因
std::map的operator[]并非单纯的查找接口:当你通过mp[key]访问时,如果该key不存在于map中,它会自动插入一个新的键值对,其中值的部分会通过默认构造函数创建。
哪怕你的代码里已经用insert插入了键1,编译器在编译mp[1]这一行时,必须确保operator[]的逻辑是合法的——也就是要保证当键不存在时,能构造出默认的F1Team对象。而你的F1Team类自定义了带参数的构造函数,这会导致编译器不再自动生成默认构造函数,因此编译阶段就会报错。
解决方案
方案1:给F1Team添加默认构造函数
显式定义一个默认构造函数,满足operator[]的要求:
class F1Team { // ... 原有成员 ... public: // 默认构造函数 F1Team() : name(""), titles(0) {} F1Team(string tnm) { name = tnm; titles = 0; } // ... 原有成员函数 ... };
方案2:改用find()查找,避免自动插入
如果你不想给类添加默认构造函数,可以使用std::map::find()方法来查找键,它不会自动插入新元素:
auto it = mp.find(1); if (it != mp.end()) { it->second.display(); }
这种方式只会查找已存在的键,无需类支持默认构造,更适合不需要自动插入的场景。
内容的提问来源于stack exchange,提问作者Priyanshu Sahani
相关产品推荐
相关产品推荐

