tags:

views:

97

answers:

3

I wrote a piece of code and used map and vector but it shows me something I can't get. I'll be thankful if someone help me in this way and correct my code or give me some hints.

The code is:

      // 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);
        } 

and the errors are for this line: position[exposition].push_back (&nb_tuple); and the errors are:

/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)

Thanks in advance. Bahar

+1  A: 

std::map is a sorted container of pairs. As such, keys in the map must have operator <() defined. Make sure Vector has the less-than operator defined.

For example:

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
}

Of course, there other ways to do this. You may make operator< a member function. Or you may have the two member data public and the operator a non-member, non-friend function. Or you may define operator< in an anonymous namespace, to enhance information hiding. Or you may use a comparator other than operator<.

wilhelmtell
Either `operator<` defined or else provide a comparison function.
David Rodríguez - dribeas
also missing from the example is pushing something into the vector 'exposition' to make it unique as a key.
Greg Domjan
@WilhelmTell: If the `Vector::operator<` was missing wouldn't `pos.insert` not compile as well since std::set uses `Vector::operator<` by default too?
Eugen Constantin Dinca
@Eugen the OP posted only one error.
wilhelmtell
Hi I want to know how should I add this operator and where?Its very important to correct this part so please tell me where I must add it to work well. Thanks
bahar
A: 

I notice that you seem to have a pushback line that compiles and a line that does not.

The difference might be that you have a const in the first case

NeighborTuple const &nb_tuple = *it;
djna
A: 

You declared the position object as followed : std::map<Vector, std::vector<const NeighborTuple *> > position;

And you are trying to push NeighborTuple * inside...

Try using const NeighborTuple *

Xavier V.
Thanks so much it worked.
bahar
You're welcome !
Xavier V.