套接字映射定义:
map<unsigned char[6],RawSocket> sockets_;
为套接字映射设置值:
RawSocket tmp(ifcName,newSocketsIt->src_mac_);
sockets_[newSocketsIt->src_mac_] = tmp;
原始套接字定义:
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