Topic: templatized iterator being treated as a const_iterator


Author: "Kory Yingling" <mister2zx3@yahoo.com>
Date: Fri, 5 Apr 2002 21:51:40 GMT
Raw View
What must I do to get listIter to behave as an iterator and not a
const_iterator?

 template <class L> // return -1 if wrapped, 0 if it is the next item, and 1
if it stays at the edge.
  int nextInList ( const bool& wrap, const L& theList, L::iterator&
listIter )
   {
    ++listIter;
    if ( listIter == theList.end() ) {
     if ( wrap ) {
      listIter = theList.begin();
      return -1;
     } else {
      --listIter;
      return 1;
     }
    }
    return 0;
   }
 template <class L> // return -1 if wrapped, 0 if it is the prev item, and 1
if it stays at the edge.
  int prevInList ( const bool& wrap, const L& theList, L::iterator*
listIter )
   {
    if ( listIter == theList.begin() ) {
     if ( wrap ) {
      listIter = theList.end();
      --listIter;
      return -1;
     }
    } else {
     --listIter;
     return 0;
    }
    return 1;
   }

All help much appreciated!
This has eaten up my whole day..


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Matt Ferreira" <mferreira@comcast.net>
Date: Sat, 6 Apr 2002 16:24:59 GMT
Raw View
>   int nextInList ( const bool& wrap, const L& theList, L::iterator&
> listIter )
>    {
>     ++listIter;
>     if ( listIter == theList.end() ) {
>      if ( wrap ) {
>       listIter = theList.begin();
               ^^^^^^^
I would say that this assignment here is the culprit. theList is declared as
const in your parameter list, so all functions that act on it must be const.
theList.begin() is returning a const_iterator because of this.

>       return -1;
>      } else {
>       --listIter;
>       return 1;
>      }
>     }
>     return 0;
>    }


--

===================
Matt Ferreira
mferreira@comcast.net



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]