2010-09-08 3 views
0

Я написал фрагмент кода и использовал карту и вектор, но он показывает мне то, что я не могу получить. Я буду благодарен, если кто-то поможет мне таким образом, исправьте мой код или дайте мне несколько советов.Ошибки при использовании map() в C++

Код:

 // 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); и ошибки:

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

Спасибо заранее. Bahar

ответ

0

Вы объявившего объект позиции, как следует: std::map<Vector, std::vector<const NeighborTuple *> > position;

И вы пытаетесь толкать NeighborTuple * внутри ...

Попробуйте использовать const NeighborTuple *

+0

Спасибо, что это сработало. – bahar

+0

Добро пожаловать! –

1

std::map - сортированный контейнер из пар. Таким образом, ключи на карте должны иметь operator <(). Удостоверьтесь, что Vector имеет заданный оператор меньше.

Например: есть

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<.

+0

Либо 'operator <' defined, либо также предоставляет функцию сравнения. –

+0

, также отсутствующий в этом примере, толкает что-то в векторную «экспозицию», чтобы сделать его уникальным как ключ. –

+0

Если оператор 'Vector :: operator <' отсутствовал, то 'pos.insert' не будет компилироваться, так как std :: set также использует' Vector :: operator <'по умолчанию тоже? –

0

Я замечаю, что у вас, кажется, есть линия pushback, которая компилируется, и линия, которая этого не делает.

Разница может быть, что у вас есть константный в первом случае

NeighborTuple const &nb_tuple = *it; 
Смежные вопросы