使用自定义运算符时出错<与std :: less [英] Error using custom operator< with std::less

查看:108
本文介绍了使用自定义运算符时出错<与std :: less的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重载< 运算符,但遇到问题。



这是我的实现:

  int Vector3D :: operator< (const Vector3D& vector)
{
if(x< vector.x)
return 1;
否则
返回0;
}

我使用以下代码称呼它:

  std :: map< Vector3D,std :: vector< const NeighborTuple *> >位置; 
std :: set< Vector3D> pos;
for(NeighborSet :: iterator it = N.begin(); it!= N.end(); it ++)
{
NeighborTuple const& nb_tuple = * it;

矢量博览会;
pos.insert(博览会);
position [exposition] .push_back(& nb_tuple);
}

但我收到此错误:


/ usr / include / c ++ / 4.1.2 / bits / stl_function.h:在成员函数'bool std :: less< _Tp> :: operator()(const _Tp& ,const _Tp&)const [with _Tp = ns3 :: Vector3D]':

/usr/include/c++/4.1.2/bits/stl_map.h:347:从'_Tp& std :: map< _Key,_Tp,_Compare,_Alloc> :: operator [](const _Key&)[with _Key = ns3 :: Vector3D,_Tp = std :: vector< const ns3 :: olsr :: NeighborTuple *,std :: allocator< const ns3 :: olsr :: NeighborTuple *> > ;, _Compare = std :: less< ns3 :: Vector3D> ;, _Alloc = std :: allocator< std :: pair< const ns3 :: Vector3D,std :: vector< const ns3 :: olsr :: NeighborTuple *,std :: allocator< const ns3 :: olsr :: NeighborTuple *> > > >]'

../src/routing/olsr/olsr-routing-protocol.cc:853:从此处实例化

/usr/include/c++/4.1.2 /bits/stl_function.h:227:错误:将'const ns3 :: Vector3D'传递为'int ns3 :: Vector3D :: operator<(const ns3 :: Vector3D&)'的'this'自变量



解决方案

错误


将'const ns3 :: Vector3D'作为
传递'int'
ns3 :: Vector3D :: operator<(const
ns3 :: Vector3D&)'的此参数将丢弃限定符


表示您的运算符< 不保证会赢得比较请勿修改左手参数,而地图要求比较操作不得修改任何内容,并尝试将此运算符用于常量实例(地图将键类型存储为const对象)。



简而言之,此类运算符重载一定不能突变任何东西,并且两个操作数都必须声明为const。由于已将此重载作为成员函数,因此必须使函数本身成为常量。

  bool运算符<(const ns3 :: Vector3D& rhs)const; 

BTW,为什么不返回布尔值(结果必须为true或false)? / p>

I am trying to overload the < operator, but running into a problem.

Here is my implementation:

int Vector3D::operator < (const Vector3D &vector)
{
   if(x<vector.x)
       return 1;
   else
       return 0;
}

I am calling it using this code:

std::map<Vector3D, std::vector<const NeighborTuple *> > position; 
std::set<Vector3D> pos; 
for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
{
    NeighborTuple const  &nb_tuple = *it;

    Vector exposition;
    pos.insert (exposition);
    position[exposition].push_back (&nb_tuple);
}

But I get this error:

/usr/include/c++/4.1.2/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = ns3::Vector3D]’:
/usr/include/c++/4.1.2/bits/stl_map.h:347: instantiated from ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = ns3::Vector3D, _Tp = std::vector<const ns3::olsr::NeighborTuple*, std::allocator<const ns3::olsr::NeighborTuple*> >, _Compare = std::less<ns3::Vector3D>, _Alloc = std::allocator<std::pair<const ns3::Vector3D, std::vector<const ns3::olsr::NeighborTuple*, std::allocator<const ns3::olsr::NeighborTuple*> > > >]’
../src/routing/olsr/olsr-routing-protocol.cc:853: instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:227: error: passing ‘const ns3::Vector3D’ as ‘this’ argument of ‘int ns3::Vector3D::operator<(const ns3::Vector3D&)’ discards qualifiers

解决方案

The error

passing ‘const ns3::Vector3D’ as ‘this’ argument of ‘int ns3::Vector3D::operator<(const ns3::Vector3D&)’ discards qualifiers

indicates that your operator< does not make the promise that the comparison won't modify the left-hand argument, whereas the map requires that the comparison operation shouldn't modify anything and is trying to use this operator for a constant instance (the map stores the key-types as const objects).

In short, such operator overloads must not mutate anything, and both operands must be declared const. As you have overloaded this as a member function, you must make the function itself const.

bool operator<(const ns3::Vector3D& rhs) const;

BTW, why don't you return a bool (the result must be true or false)?

这篇关于使用自定义运算符时出错&lt;与std :: less的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆