首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C++中使用map()时出现错误

在C++中使用map()时出现错误
EN

Stack Overflow用户
提问于 2010-09-09 04:21:10
回答 3查看 392关注 0票数 0

我写了一段代码,使用了地图和矢量,但它显示了一些我无法获得的东西。如果有人以这种方式帮助我并更正我的代码或给我一些提示,我将不胜感激。

代码是:

代码语言:javascript
复制
      // For each node in N, calculate the reachability, i.e., the
      // number of nodes in N2 which are not yet covered by at
      // least one node in the MPR set, and which are reachable
      // through this 1-hop neighbor
      std::map<int, std::vector<const NeighborTuple *> > reachability;
      std::set<int> rs;
      for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
        {
          NeighborTuple const &nb_tuple = *it;
          int r = 0;
          for (TwoHopNeighborSet::iterator it2 = N2.begin (); it2 != N2.end (); it2++)
            {
              TwoHopNeighborTuple const &nb2hop_tuple = *it2;
              if (nb_tuple.neighborMainAddr == nb2hop_tuple.neighborMainAddr)
                r++;
            }
          rs.insert (r);
          reachability[r].push_back (&nb_tuple);
        }
/*******************************************************************************/
//for keepping exposition of a node
        std::map<Vector, std::vector<const NeighborTuple *> > position; 
        std::set<Vector> pos; 
        for (NeighborSet::iterator it = N.begin(); it != N.end(); it++)
        {
          NeighborTuple   nb_tuple = *it;

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

错误是针对这一行的:position[exposition].push_back (&nb_tuple);,错误是:

代码语言:javascript
复制
/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<_K
ey, _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: no match for ‘operator<’ in ‘__x < __y’
debug/ns3/ipv6-address.h:432: note: candidates are: bool ns3::operator<(const ns3::Ipv6Address&, const ns3::Ipv6Address&)
debug/ns3/nstime.h:475: note:                 bool ns3::operator<(const ns3::Time&, const ns3::Time&)
debug/ns3/ipv4-address.h:305: note:                 bool ns3::operator<(const ns3::Ipv4Address&, const ns3::Ipv4Address&)
debug/ns3/address.h:231: note:                 bool ns3::operator<(const ns3::Address&, const ns3::Address&)
debug/ns3/type-id.h:376: note:                 bool ns3::operator<(ns3::TypeId, ns3::TypeId)

提前谢谢。巴哈

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-09 04:35:09

您声明的position对象如下:std::map<Vector, std::vector<const NeighborTuple *> > position;

你想把NeighborTuple *推进去..。

尝试使用const NeighborTuple *

票数 0
EN

Stack Overflow用户

发布于 2010-09-09 04:27:37

std::map是对的有序容器。因此,映射中的键必须定义了operator <()。确保Vector定义了小于运算符。

例如:

代码语言:javascript
复制
class Vector {
    int len, ang;
    friend bool operator<(const Vector&, const Vector&);
};

bool operator<(const Vector& v1, const Vector& v2)
{
    return true_if_v1_is_less_than_v2();  // you define what "less than" means
}

当然,还有其他方法可以做到这一点。您可以使operator<成为成员函数。或者,您可以将两个成员数据设置为公共数据,并将运算符设置为非成员、非友元函数。或者,您可以在匿名名称空间中定义operator<,以增强信息隐藏。或者,您可以使用operator<以外的比较器。

票数 1
EN

Stack Overflow用户

发布于 2010-09-09 04:28:37

我注意到你似乎有一个可以编译的推回行和一个不能编译的推回行。

不同之处可能在于第一种情况下有一个const

代码语言:javascript
复制
NeighborTuple const &nb_tuple = *it;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3671584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档