1
I am developing an object-oriented library. I created an iterator for my future container. I have advance_iterator
and regress_iterator
. Both give rise to bidirectional_iterator
. The code is:
#ifndef OBJSTD_ADVANCE_ITERATOR
#define OBJSTD_ADVANCE_ITERATOR
#include <cstddef>
namespace objstd
{
template <typename data_type> class advance_iterator
{
public:
advance_iterator(data_type* data_ref)
{
this->data_reference = data_ref;
}
advance_iterator(const advance_iterator& it) = default;
advance_iterator(advance_iterator&& it) = default;
virtual bool operator > (const advance_iterator& it) const
{
return this->data_reference > it.data_reference;
}
virtual bool operator < (const advance_iterator& it) const
{
return this->data_reference < it.data_reference;
}
bool operator == (const advance_iterator& it) const
{
return this->data_reference == it.data_reference;
}
bool operator != (const advance_iterator& it) const
{
return this->data_reference != it.data_reference;
}
virtual bool operator >= (const advance_iterator& it) const
{
return (*this).operator>(it) || (*this).operator==(it);
}
virtual bool operator <= (const advance_iterator& it)const
{
return (*this).operator<(it) || (*this).operator==(it);
}
virtual advance_iterator& operator+=(const size_t skips)
{
this->data_reference += skips;
return *this;
}
virtual advance_iterator& operator++()
{
*this += 1;
return *this;
}
virtual advance_iterator& operator++(int)
{
advance_iterator& state = *this;
this->operator++();
return state;
}
virtual advance_iterator operator+ (const size_t skips)
{
return this->data_reference + skips;
}
//must be declared virtual for non-conservational
virtual data_type& operator*() const
{
return *this->data_reference;
}
protected:
data_type* data_reference;
};
}
#endif // OBJSTD_ADVANCE_ITERATOR
regress_iterator
#ifndef OBJSTD_REGRESS_ITERATOR
#define OBJSTD_REGRESS_ITERATOR
#include <cstddef>
namespace objstd
{
template <typename data_type> class regress_iterator
{
public:
regress_iterator(data_type* data_ref)
{
this->data_reference = data_ref;
}
regress_iterator(const regress_iterator& it) = default;
regress_iterator(regress_iterator&& it) = default;
virtual bool operator > (const regress_iterator& it) const
{
return this->data_reference > it.data_reference;
}
virtual bool operator < (regress_iterator& it) const
{
return this->data_reference < it.data_reference;
}
bool operator == (regress_iterator& it) const
{
return this->data_reference == it.data_reference;
}
bool operator != (regress_iterator& it) const
{
return this->data_reference != it.data_reference;
}
virtual bool operator >= (regress_iterator& it) const
{
return (*this).operator>(it) || (*this).operator==(it);
}
virtual bool operator <= (regress_iterator& it)const
{
return (*this).operator<(it) || (*this).operator==(it);
}
virtual regress_iterator& operator-=(size_t backwards)
{
this->data_reference -= backwards;
return *this;
}
virtual regress_iterator& operator--()
{
*this -= 1;
return *this;
}
virtual regress_iterator& operator--(int)
{
regress_iterator& state = *this;
this->operator--();
return state;
}
virtual regress_iterator operator- (size_t backwards)
{
return this->data_reference - backwards;
}
//must be declared virtual for non-conservational
virtual data_type& operator*() const
{
return *this->data_reference;
}
protected:
data_type* data_reference;
};
}
#endif // OBJSTD_REGRESS_ITERATOR
And bidirectional_iterator
is defined as such:
#ifndef OBJSTD_BIDIRECTIONAL_ITERATOR
#define OBJSTD_BIDIRECTIONAL_ITERATOR
#include "regress_iterator.hpp"
#include "advance_iterator.hpp"
namespace objstd
{
template <typename data_type> class bidirectional_iterator : public advance_iterator <data_type>, public regress_iterator <data_type>
{
};
}
#endif // OBJSTD_BIDIRECTIONAL_ITERATOR
The problem is creating an instance like bidirectional_iterator(ponteiro)
, because the compiler doesn’t know how to boot. What I’m doing wrong?
I’ve already tried to do that, but I got the bug that there are no such members in the class.
– user2692
@Lucashenrique Opa, missed the template argument there at startup. Fixed.
– Guilherme Bernal
Now I understand; thank you. I’ll check after I sleep tired^.
– user2692
William, some mistakes appeared to me due to ambiguity and I don’t know what to do. You can help me?
– user2692
@Lucashenrique, are you not having some compiler problem? I tested William’s code is here and I made no mistake...
– Massa
@Mass he updated before your comment.
– user2692
So I think you can mark his answer as the correct one! D
– Massa