Estoy tratando de convertir una clase de iterador Tengo que ser stl compatible para que pueda ser utilizado con los algoritmos STL. En el siguiente ejemplo sencillo (y francamente inútil), que debe imprimir los valores de 0 a 5 inclusive, que estoy consiguiendo los errores siguientes,Escribiendo stl compatible con iteradores
ISO C++ prohíbe incrementar un puntero del tipo 'iterador (*)()'
y,
conversiónno válido de 'iterador (*)()' a 'int'
¿Qué estoy haciendo mal?
Gracias.
#include <iterator>
#include <algorithm>
#include <iostream>
class Iterator : public std::iterator<std::bidirectional_iterator_tag, int> {
public:
Iterator(int i = 0) : val(i) {
if(val<0 || val>5) throw;
}
bool operator==(Iterator const& rhs) const {
return (val==rhs.val);
}
bool operator!=(Iterator const& rhs) const {
return !(*this==rhs);
}
Iterator& operator++() {
if(val!=6)
++val;
return *this;
}
Iterator operator++(int) {
Iterator tmp (*this);
++(*this);
return tmp;
}
Iterator& operator--() {
if(val!=-1)
--val;
return *this;
}
Iterator operator--(int) {
Iterator tmp (*this);
--(*this);
return tmp;
}
int operator*() const {
if(val==-1 || val==6) throw;
return val;
}
private:
int val;
};
Iterator begin() {
return Iterator();
}
Iterator end() {
return ++Iterator(5);
}
void print(int i) {
std::cout << i << std::endl;
}
int main(int argc, char* argv[]) {
std::for_each(begin,end,print);
}
¿Está utilizando esto para una clase de contenedor personalizado? – Alerty