Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compare std::list iterator to list::back()?

Tags:

c++

templates

stl

I have a custom iterator template class that wraps up a std::list iterator. In my hasNext() method I want to check to see if the iterator refers to the last item in the list. However this code:

template< class Type >
...
virtual bool hasNext( void )
{
    return itTypes != pList->back();
}
...
typename std::list< Type > * pList;
typename std::list< Type >::iterator itTypes;

Produces the error:

error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_List_iterator<_Mylist>'

If I compare itTypes to list::begin() or list::end() I get no problem. The documentation I've looked at for STL iterators says that the bidirectional iterators used by std::list support the equality and inequality comparison operators. Is there a reason I can't compare to list::back() then?

like image 550
PeddleSpam Avatar asked Mar 17 '26 07:03

PeddleSpam


2 Answers

You want end(). back() gets the value of the last element, which is not an iterator into the list at all.

like image 192
Potatoswatter Avatar answered Mar 19 '26 09:03

Potatoswatter


the back() return the element not the iterator. so use code

return *itTypes != pList->back()
like image 43
RolandXu Avatar answered Mar 19 '26 08:03

RolandXu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!