3

套接字映射定义:

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
4

2 回答 2

5
sockets_[newSocketsIt->src_mac_] = tmp;

map'soperator[]正在尝试使用该键默认构造一个元素,因为它还没有在地图中。tmp然后它将通过分配给它operator=。您要么需要提供默认构造函数,RawSocket要么std::map::insert()改为使用;

你得到一个错误,insert因为 unsigned[6] 和src_mac_都是数组类型,你不能用另一个数组初始化一个数组(当你在做时会发生这种情况std::pair)。考虑切换到 std::array 或 std::vector 之类的容器,或者将数组包装在您自己的结构中并将其用作键。

快速解决方法是:

struct Arr6 {
    unsigned char array[6];
};

std::map <Arr6, RawSocket> sockets_;

也将 的定义更改src_mac_Arr6

于 2012-08-19T11:22:19.003 回答
1

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

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

错误会消失

于 2012-08-19T11:29:08.140 回答