Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么使用std::map的代码无法编译?_C++_Constructor_Stdmap - Fatal编程技术网

C++ 为什么使用std::map的代码无法编译?

C++ 为什么使用std::map的代码无法编译?,c++,constructor,stdmap,C++,Constructor,Stdmap,我正在尝试使用std::map。但我面临一个编译问题。 守则: // main.cpp #include <iostream> #include <map> class Student { public: // with follow line there will be no compile error // explicit Student(){} explicit Student(std::string name_, int grade_

我正在尝试使用std::map。但我面临一个编译问题。 守则:

// main.cpp
#include <iostream>
#include <map>

class Student
{
public:
    // with follow line there will be no compile error
    // explicit Student(){}

    explicit Student(std::string name_, int grade_)
        : name(name_), grade(grade_)
    {
    }

    ~Student()
    {
    }

private:
    std::string name;
    int grade;
};

int main(int argc, char **argv)
{
    std::map<std::string, Student> student_map;
    Student x("mike", 10);
    student_map.insert(std::pair<std::string, Student>("mike", x));
    Student y = student_map["mike"];
    return 0;
}
但是编译人员说

In file included from /usr/include/c++/7/bits/stl_map.h:63:0,
                 from /usr/include/c++/7/map:61,
                 from main.cpp:2:
/usr/include/c++/7/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 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Student]’:
/usr/include/c++/7/tuple:1641:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; _Args2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Student]’
/usr/include/c++/7/ext/new_allocator.h:136:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const std::__cxx11::basic_string<char>, Student>; _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Student> >]’
/usr/include/c++/7/bits/alloc_traits.h:475:4:   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 std::__cxx11::basic_string<char>, Student>; _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Student> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Student> > >]’
/usr/include/c++/7/bits/stl_tree.h:626: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<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Key = std::__cxx11::basic_string<char>; _Val = std::pair<const std::__cxx11::basic_string<char>, Student>; _KeyOfValue = std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Student> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Student> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Student> >*]’
/usr/include/c++/7/bits/stl_tree.h:643: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<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Key = std::__cxx11::basic_string<char>; _Val = std::pair<const std::__cxx11::basic_string<char>, Student>; _KeyOfValue = std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Student> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Student> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Student> >*]’
/usr/include/c++/7/bits/stl_tree.h:2398: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<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Key = std::__cxx11::basic_string<char>; _Val = std::pair<const std::__cxx11::basic_string<char>, Student>; _KeyOfValue = std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Student> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Student> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, Student> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, Student> >]’
/usr/include/c++/7/bits/stl_map.h:512:8:   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 = std::__cxx11::basic_string<char>; _Tp = Student; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Student> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Student; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::__cxx11::basic_string<char>]’
main.cpp:29:35:   required from here
/usr/include/c++/7/tuple:1652:70: error: no matching function for call to ‘Student::Student()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
main.cpp:10:14: note: candidate: Student::Student(std::__cxx11::string, int)
     explicit Student(std::string name_, int grade_)
              ^~~~~~~
main.cpp:10:14: note:   candidate expects 2 arguments, 0 provided
main.cpp:4:7: note: candidate: Student::Student(const Student&)
 class Student
       ^~~~~~~
main.cpp:4:7: note:   candidate expects 1 argument, 0 provided
我尝试添加空构造函数,代码现在可以编译了

我的问题是:为什么类必须有空构造函数

我有什么误解吗

student_map["mike"]
这可能会添加类型为
Student
的值初始化元素,这是不可能的。使用

student_map.at("mike")

相反。

std::map
操作符[]
如果键在映射中不存在,则默认构造一个
映射类型


如果您使用map的
,则不需要默认构造函数。查找

提供一个副本构造函数或删除
explicit
关键字。@特洛伊木马否。问题是
[]
。我试图删除explicit,但编译仍有很多问题:(对不起,如果地图上没有钥匙,我就不太懂
这个词了。
。我已经插入了“mike”,为什么钥匙不存在?@XuHui
操作符[]里面的代码
是一个
if
语句。如果键存在,则返回找到的对象。否则返回一个新的默认构造对象。要编译该代码,需要定义默认构造函数。这与编写以下代码类似:
if(false)foo()
对于要编译的代码,需要定义
foo
,即使程序运行时永远不会调用
foo
。您的意思是,
student\u map[“mike”]
将向映射中添加一个student对象,并且关键字是“mike”?因为已经有一个带有关键字“mike”的对象了,所以编译失败了?我理解的对吗?@XuHui在编译时,编译器不知道是否真的存在这样的元素,所以它必须假设为最坏的情况。但是为什么使用默认构造函数,代码可以编译正常?@XuHui如果提供默认构造函数,就可以构造新元素(
Student()
)。
student_map["mike"]
student_map.at("mike")