C++ 不能使用map,除非我为Custom\u Class定义了没有参数的默认构造函数
除非我执行以下两项操作之一,否则上述代码不起作用:- 1删除map的用法 2定义一个不要求参数的空构造函数 是否使用了空构造函数?我试着做一个coutdefault,如果值在映射中不存在,则构造该值,这就是它不起作用的原因 使用或替代: 无效生成房屋字符串cName { Cat-pcName; 这个->Cats.push_backp; 此->CatName\u Cat.insert\u或\u assignp.name,p; }C++ 不能使用map,除非我为Custom\u Class定义了没有参数的默认构造函数,c++,oop,constructor,C++,Oop,Constructor,除非我执行以下两项操作之一,否则上述代码不起作用:- 1删除map的用法 2定义一个不要求参数的空构造函数 是否使用了空构造函数?我试着做一个coutdefault,如果值在映射中不存在,则构造该值,这就是它不起作用的原因 使用或替代: 无效生成房屋字符串cName { Cat-pcName; 这个->Cats.push_backp; 此->CatName\u Cat.insert\u或\u assignp.name,p; } 如前所述,如果元素不存在,map的方括号操作符default将构造该
如前所述,如果元素不存在,map的方括号操作符default将构造该元素。因此,如果调用该运算符,则映射类型必须是默认可构造的 这是因为map对象尝试默认初始化Cat对象。因此,在这里定义默认构造函数是一种方法。只需执行Cat=default,我知道它正在尝试这样做,但它在哪里以及为什么要尝试这样做?为什么在我没有插入或要求它这样做的情况下,它会默认初始化Cat对象。是否创建默认对象来填充某些位置?如果是,那么为什么呢?因为无论何时创建对象,构造函数都会在幕后被调用。因此,当调用map[]时,它尝试初始化Cat对象,但失败了,因为它需要一个默认的构造函数。我们的问题是从运行时的角度来看的。跟踪代码不显示正在使用的默认构造函数。但是错误消息是在编译时发出的。[]运算符需要一个默认构造函数,该函数将作为映射模板的一部分生成和编译,即使您的代码从未调用它。
#include<bits/stdc++.h>
using namespace std;
class Cat
{
public:
string name;
// Cat(){}
Cat(string name_given)
{
this->name=name_given;
}
};
class House
{
public:
vector<Cat> Cats;
map<string,Cat> CatName_Cat;
House(string CatName)
{
generateHouse(CatName);
}
private:
void generateHouse(string cName)
{
Cat p(cName);
this->Cats.push_back(p);
this->CatName_Cat[p.name]=p;
}
};
int main()
{
Cat p("McCat");
}
In file included from /usr/include/c++/7/functional:54:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:71,
from dumber.cpp:1:
/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 = {const 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 = Cat]’:
/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 = {const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}; _Args2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = Cat]’
/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>, Cat>; _Args = {const std::piecewise_construct_t&, std::tuple<const 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>, Cat> >]’
/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>, Cat>; _Args = {const std::piecewise_construct_t&, std::tuple<const 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>, Cat> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Cat> > >]’
/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<const 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>, Cat>; _KeyOfValue = std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Cat> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Cat> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Cat> >*]’
/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<const 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>, Cat>; _KeyOfValue = std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Cat> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Cat> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const std::__cxx11::basic_string<char>, Cat> >*]’
/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<const 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>, Cat>; _KeyOfValue = std::_Select1st<std::pair<const std::__cxx11::basic_string<char>, Cat> >; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Cat> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, Cat> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, Cat> >]’
/usr/include/c++/7/bits/stl_map.h:493:8: required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = Cat; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, Cat> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Cat; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::__cxx11::basic_string<char>]’
dumber.cpp:30:28: required from here
/usr/include/c++/7/tuple:1652:70: error: no matching function for call to ‘Cat::Cat()’
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
^
dumber.cpp:9:3: note: candidate: Cat::Cat(std::__cxx11::string)
Cat(string name_given)
^~~
dumber.cpp:9:3: note: candidate expects 1 argument, 0 provided
dumber.cpp:4:7: note: candidate: Cat::Cat(const Cat&)
class Cat
^~~
dumber.cpp:4:7: note: candidate expects 1 argument, 0 provided
dumber.cpp:4:7: note: candidate: Cat::Cat(Cat&&)
dumber.cpp:4:7: note: candidate expects 1 argument, 0 provided