c++ - iterator class: compiler error in g++ while doing iterator add or subtract (works in VS2010) -
c++ - iterator class: compiler error in g++ while doing iterator add or subtract (works in VS2010) -
i implementing array in c++ (for various reasons, 1 of them know custom iterators).
while testing out noticed not compile in g++ 4.4 works fine in visual studio 2010.
i have included illustration programme below. in that, print out lastly 1 value of toy array implemented using templates accompanying iterator class. compiler error following:
$ g++-4.4 -ansi -wall -std=c++0x mybuffer.cpp -o mybuffer in file included /usr/include/c++/4.4/bits/stl_algobase.h:69, /usr/include/c++/4.4/memory:49, mybuffer.cpp:1: /usr/include/c++/4.4/bits/stl_iterator.h: in fellow member function ‘std::reverse_iterator<_iterator> std::reverse_iterator<_iterator>::operator+(typename std::iterator_traits<_iter>::difference_type) const [with _iterator = cmyitr<cmybuff<double>, double>]’: mybuffer.cpp:205: instantiated here /usr/include/c++/4.4/bits/stl_iterator.h:221: error: passing ‘const cmyitr<cmybuff<double>, double>’ ‘this’ argument of ‘cmyitr<t, typename t::value_type> cmyitr<t, elem_type>::operator-(typename t::difference_type) [with t = cmybuff<double>, elem_type = double]’ discards qualifiers
the error occurs if *(ritr+1) operation, not if *(++ritr) operation. works in visual studio 2010 in either case however.
i same compiler error in g++ 2.95 also. haven't tried in visual studio 6.
can explain doing wrong , how prepare it?
thanks. ps: using g++ 4.6 whole problem, later.
//-------------------------------------------- illustration code ----------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------// #include <memory> #include <iostream> #include <iterator> template < typename t, typename elem_type=typename t::value_type> class cmyitr { public: typedef t bufftype; typedef cmyitr<t> self_type; typedef cmyitr<self_type, elem_type> iterator; typedef typename std::bidirectional_iterator_tag iterator_category; typedef typename bufftype::value_type value_type; typedef typename bufftype::size_type size_type; typedef typename bufftype::pointer pointer; typedef typename bufftype::const_pointer const_pointer; typedef typename bufftype::reference reference; typedef typename bufftype::const_reference const_reference; typedef typename bufftype::difference_type difference_type; cmyitr( bufftype *pb, size_type pos): ptritr_(pb), ptrpos_(pos){ }; friend class cmyitr< const t, const elem_type>; elem_type &operator*(){ homecoming (*ptritr_)[ptrpos_]; }; elem_type *operator->(){ homecoming &(operator*()); }; self_type & operator++(){ ++ptrpos_; homecoming *this; }; self_type operator++(int){ self_type tmp(*this); ++(*this); homecoming tmp; }; self_type operator+(difference_type n) { self_type tmp(*this); tmp.ptrpos_ = tmp.ptrpos_ + n; homecoming tmp; }; self_type &operator+=(difference_type n){ ptrpos_ = ptrpos_ + n; homecoming *this; }; self_type & operator--(){ --ptrpos_; homecoming *this; }; /*! decrement operator decrements position index. */ self_type operator--(int){ self_type tmp(*this); --(*this); homecoming tmp; }; self_type operator-(difference_type n) { self_type tmp(*this); tmp.ptrpos_ = tmp.ptrpos_ - n; homecoming tmp; }; self_type &operator-=(difference_type n){ ptrpos_ -= n; homecoming *this; }; bool operator!=(const self_type &other) const { homecoming ptrpos_ != other.ptrpos_ && ptritr_ == other.ptritr_; }; bool operator==(const self_type &other) const { homecoming ptrpos_ == other.ptrpos_ && ptritr_ == other.ptritr_; }; private: bufftype * ptritr_; size_type ptrpos_; }; //----------------------------------------------------------------------// //----------------------------------------------------------------------// template < typename t > class cmybuff { public: enum {default_size = 4 }; typedef cmybuff<t> self_type; typedef t value_type; typedef t & reference; typedef const t & const_reference; typedef t * pointer; typedef const t * const_pointer; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef cmyitr<self_type> iterator; typedef cmyitr<const self_type> const_iterator; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const iterator> const_reverse_iterator; /*! starting forwards iterator.*/ iterator begin(){ homecoming iterator(this, 0); }; /*! forwards iterator should go till here.*/ iterator end(){ homecoming iterator(this, size_); }; /*! starting constant forwards iterator.*/ const_iterator begin() const { homecoming const_iterator(this, 0); }; /*! constant forwards iterator should go till here.*/ const_iterator end() const { homecoming const_iterator(this, size_); }; /*! reverse iterator starts here.*/ reverse_iterator rbegin(){ homecoming reverse_iterator(end()); } /*! reverse iterator end.*/ reverse_iterator rend() { homecoming reverse_iterator(begin()); } /*! constant reverse iterator starting point.*/ const_reverse_iterator rbegin() const { homecoming const_reverse_iterator(end()); } /*! constant reverse iterator should end here.*/ const_reverse_iterator rend() const { homecoming const_reverse_iterator( begin()); } /* ctor buffer*/ explicit cmybuff(size_type capacity = default_size): ptr_(null), size_(capacity) { ptr_ = new value_type [sizeof(value_type) * size_]; ptr_[0] = 0; ptr_[1] = 1; ptr_[2] = 8; ptr_[3] = 27; }; ~cmybuff() { delete [] ptr_; } reference operator[](size_type i){ homecoming ratunchecked(i); }; const_reference operator[](size_type i) const { homecoming ratunchecked(i); }; size_type size() const { homecoming size_; }; reference ratunchecked(size_type k) const { homecoming ptr_[k]; }; private: pointer ptr_; size_type size_; }; //------------------------------------------------------------------// //----------------------------------------- main ------------------// // utilize next command line compile: // g++-4.4 -ansi -wall -std=c++0x mybuffer.cpp -o mybuffer int main(){ cmybuff<double> buffer; cmybuff < double >::reverse_iterator ritr = buffer.rbegin(); //prints lastly 1 element std::cout << *(++ritr) << std::endl; // next doesn't compile on g++. const related error // containing "discards qualifier" //std::cout << *(ritr + 1) << std::endl; homecoming 0; } //-------------------------------------------------------------------------------//
//-------------------------------------------- code end ----------------//
this may not answer, can seek following:
change,difference_type n
const difference_type &n
declare operator +()
, operator -()
const
(as don't impact this
) see if helps. c++ iterator
Comments
Post a Comment