views:

310

answers:

2

Hello,

My code is using std::count() on a list of an abstract data type that i have defined. (Sommet or Edge in english). But it doesn't work, although i've overloaded the < and == operators like this :

bool operator< (const Sommet &left, const Sommet &right)
{
  if(left.m_id_sommet < right.m_id_sommet)
    return true;

  return false;
}

bool operator== (const Sommet &left, const Sommet &right)
{
  if(left.m_id_sommet == right.m_id_sommet)
    return true;

  return false;
}

Just notice that this worked using std::sort() and std::unique().

The errors are:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function 'typename std::iterator_traits<_Iterator>::difference_type std::count(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = __gnu_cxx::__normal_iterator<Sommet*, std::vector<Sommet, std::allocator<Sommet> > >, _Tp = __gnu_cxx::__normal_iterator<Sommet*, std::vector<Sommet, std::allocator<Sommet> > >]':
Graphe.cpp:43:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:422: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Sommet*, _Container = std::vector<Sommet, std::allocator<Sommet> >]() == __value'
Sommet.h:7: note: candidates are: bool operator==(const Sommet&, const Sommet&)

Thanks !

EDIT

This is how i used std::count() :

 for(vector<Sommet>::iterator iter = m_sommets.begin();
      iter != s_iter_end; iter++)
  {
    iter->SetNbSuccesseurs(count(m_sommets.begin(), m_sommets.end(), iter));
  }
+1  A: 

What you need to pass to count is a value, not an iterator:

iter->SetNbSuccesseurs(count(m_sommets.begin(), m_sommets.end(), *iter));
AraK
+2  A: 

It looks like you are passing in an iterator as the last parameter to std::count whereas you need to pass in a value (by const reference).

Post edit: it looks like I was correct, you are passing iter which is an iterator. You need to dereference it first. Try passing *iter instead.

Charles Bailey
Byyy the way :p thanks a lot :)
Amokrane