stl:map插入值时出错

时间:2012-08-19 11:17:19

标签: c++ std

套接字映射def:

map<unsigned char[6],RawSocket> sockets_;

将值设置为套接字映射:

RawSocket tmp(ifcName,newSocketsIt->src_mac_);
sockets_[newSocketsIt->src_mac_] = tmp;

RawSocket def:

class RawSocket{
public:
    RawSocket(string ifcName,const unsigned char dstMac[6]);
}

我不想拥有RawSocket的默认构造函数。 错误是:

/usr/include/c++/4.6/bits/stl_map.h: In member function ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = unsigned char [6], _Tp = RawSocket, _Compare = std::less<unsigned char [6]>, _Alloc = std::allocator<std::pair<const unsigned char [6], RawSocket> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = RawSocket, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned char [6]]’:
../sockets_container.cpp:18:34:   instantiated from here
/usr/include/c++/4.6/bits/stl_map.h:453:11: error: no matching function for call to ‘RawSocket::RawSocket()’
/usr/include/c++/4.6/bits/stl_map.h:453:11: note: candidates are:
/root/workspace/networkutils/src/raw_socket.h:23:2: note: RawSocket::RawSocket(std::string, const unsigned char*)
/root/workspace/networkutils/src/raw_socket.h:23:2: note:   candidate expects 2 arguments, 0 provided
/root/workspace/networkutils/src/raw_socket.h:21:7: note: constexpr RawSocket::RawSocket(const RawSocket&)
/root/workspace/networkutils/src/raw_socket.h:21:7: note:   candidate expects 1 argument, 0 provided
/root/workspace/networkutils/src/raw_socket.h:21:7: note: constexpr RawSocket::RawSocket(RawSocket&&)
/root/workspace/networkutils/src/raw_socket.h:21:7: note:   candidate expects 1 argument, 0 provided

像这样插入:

sockets_.insert(pair<unsigned char[6],RawSocket>(newSocketsIt->src_mac_,tmp));

将因此错误而失败:

/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, _U2&&) [with _U2 = RawSocket&, <template-parameter-2-2> = void, _T1 = unsigned char [6], _T2 = RawSocket]’:
../sockets_container.cpp:19:78:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:126:45: error: array used as initializer
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U1 = unsigned char [6], _U2 = RawSocket, _T1 = const unsigned char [6], _T2 = RawSocket]’:
/usr/include/c++/4.6/bits/stl_tree.h:139:49:   instantiated from ‘std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {std::pair<unsigned char [6], RawSocket>}, _Val = std::pair<const unsigned char [6], RawSocket>]’
/usr/include/c++/4.6/ext/new_allocator.h:114:4:   instantiated from ‘void __gnu_cxx::new_allocator<_Tp>::construct(__gnu_cxx::new_allocator<_Tp>::pointer, _Args&& ...) [with _Args = {std::pair<unsigned char [6], RawSocket>}, _Tp = std::_Rb_tree_node<std::pair<const unsigned char [6], RawSocket> >, __gnu_cxx::new_allocator<_Tp>::pointer = std::_Rb_tree_node<std::pair<const unsigned char [6], RawSocket> >*]’
/usr/include/c++/4.6/bits/stl_tree.h:405:8:   instantiated from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {std::pair<unsigned char [6], RawSocket>}, _Key = unsigned char [6], _Val = std::pair<const unsigned char [6], RawSocket>, _KeyOfValue = std::_Select1st<std::pair<const unsigned char [6], RawSocket> >, _Compare = std::less<unsigned char [6]>, _Alloc = std::allocator<std::pair<const unsigned char [6], RawSocket> >, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const unsigned char [6], RawSocket> >*]’
/usr/include/c++/4.6/bits/stl_tree.h:971:66:   instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Base_ptr, _Arg&&) [with _Arg = std::pair<unsigned char [6], RawSocket>, _Key = unsigned char [6], _Val = std::pair<const unsigned char [6], RawSocket>, _KeyOfValue = std::_Select1st<std::pair<const unsigned char [6], RawSocket> >, _Compare = std::less<unsigned char [6]>, _Alloc = std::allocator<std::pair<const unsigned char [6], RawSocket> >, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned char [6], RawSocket> >, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Base_ptr = const std::_Rb_tree_node_base*]’
/usr/include/c++/4.6/bits/stl_tree.h:1285:64:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = std::pair<unsigned char [6], RawSocket>, _Key = unsigned char [6], _Val = std::pair<const unsigned char [6], RawSocket>, _KeyOfValue = std::_Select1st<std::pair<const unsigned char [6], RawSocket> >, _Compare = std::less<unsigned char [6]>, _Alloc = std::allocator<std::pair<const unsigned char [6], RawSocket> >]’
/usr/include/c++/4.6/bits/stl_map.h:526:64:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = std::pair<unsigned char [6], RawSocket>, <template-parameter-2-2> = void, _Key = unsigned char [6], _Tp = RawSocket, _Compare = std::less<unsigned char [6]>, _Alloc = std::allocator<std::pair<const unsigned char [6], RawSocket> >, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::map<_Key, _Tp, _Compare, _Alloc>::value_type>::other>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned char [6], RawSocket> >]’
../sockets_container.cpp:19:79:   instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:137:40: error: array used as initializer

2 个答案:

答案 0 :(得分:5)

sockets_[newSocketsIt->src_mac_] = tmp;

map的{​​{1}}正在尝试使用该键默认构造元素,因为它尚未在地图中。然后,它会通过operator[]为其分配tmp。您需要为operator=提供默认构造函数或使用std::map::insert()代替;

你的RawSocket收到错误,因为unsigned [6]和insert都是数组类型,你不能用另一个数组初始化一个数组(当你正在进行时会发生这种情况{ {1}})。考虑切换到像std :: array或std :: vector这样的容器,或者将数组包装在你自己的struct中并将其用作键。

快速解决方法是:

src_mac_

也将std::pair的定义更改为struct Arr6 { unsigned char array[6]; }; std::map <Arr6, RawSocket> sockets_;

答案 1 :(得分:1)

我不确定你在这里想知道什么....但是如果你要写

class RawSocket{
public:
    RawSocket(){}
    RawSocket(string ifcName,const unsigned char dstMac[6]);
}

错误将消失