user-defined iterator

This is a discussion on user-defined iterator within the C++ Programming forums, part of the General Programming Boards category; /usr/include/c++/3.2/bits/stl_iterator.h: In constructor `__gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter = Expression* const*, _Iterator = Expression**, _Container = std::vector<Expression*, std::allocator<Expression*> >]': ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    user-defined iterator

    /usr/include/c++/3.2/bits/stl_iterator.h: In constructor
    `__gnu_cxx::__normal_iterator<_Iterator,
    _Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter,
    _Container>&) [with _Iter = Expression* const*, _Iterator = Expression**,
    _Container = std::vector<Expression*, std::allocator<Expression*> >]':
    set.hpp:184: instantiated from `Set<T>::iterator Set<T>::begin() const [with T = Expression]'
    expression.cpp:99: instantiated from here
    /usr/include/c++/3.2/bits/stl_iterator.h:589: invalid conversion from `
    Expression* const* const' to `Expression**'

    Line 99 is j = n.begin()
    expression.cpp:
    Code:
    void Expression::substitute(const Set<Expression>& exp, const Set<Variable_Plain>& var) {
      Set<Variable_Plain>::iterator i;
      Set<Expression>::iterator j;
      Set<Expression> n(exp);
      Set<Variable_Plain> v(var);
      for (i = v.begin(),
    	 j = n.begin();
           (i != v.end())
    	 && (j != n.end());
           ++i,++j) {
        //    std::cerr << "beg"<<std::endl;
        Factor_Expression f_e(*j);
        Variable vv(*i);
        algo::for_each(terms.begin(),terms.end(),&Expression::sub_substitute,this,f_e,vv);
        //    std::cerr << "end"<<std::endl;
      }
      //  std::cerr << "end substitute" << std::endl;
    }
    set.hpp
    line 184: is "i.it = (members.begin());"
    Code:
    template<typename T>
    class Set {
    //...
      class iterator {
        public:
          typename std::vector<T*>::iterator it;
        public:
          //iterator(const typename std::vector<T*>::iterator& i) : it(i) {}
          T& operator*() const {
            return **it;
          }
          T operator++() {
            ++it;
            return **this;
          }
          T& operator++(int)  {
            it++;
            return **this;
          }
          T operator--() {
            --it;
            return **this;
          }
          T& operator--(int) {
            it--;
            return **this;
          }
          iterator operator+(int x) const {
            iterator i(it);
            for (int n=0;n<x;++n)
    	  ++i;
            return i;
          }
          const bool operator==(const iterator& i) const {
            return (it == i.it);
          }
          const bool operator!=(const iterator& i) const {
            return !(*this == i);
          }
          iterator operator-(int x) const {
            iterator i(it);
            for (int n=0;n<x;++n)
    	  --i;
            return i;
          }
        };
    //...
      iterator begin() const {
        iterator i;
        i.it = (members.begin());
        return i;
      }
      iterator end() const {
        iterator i;
        i.it = (members.end());
        return i;
      }
    };
    My question is... why is it giving me that error?
    invalid conversion from Expression* const* const to Expression**
    Wouldn't it cast itself implicitly?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Oddly enough, I've dealt with almost identical errors at work lately, and they truly are a pain to debug. The problem (most likely) is this:

    Your iterator class is allowed to modify the data it is pointing to. Your begin() function, however, is const. Hence, an internal contradiction (which naturally produces the most undecipherable error message).

    As a solution, I'd provide a non-const version of begin and end for your set.

    Hope this solves it for you.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Thanks, I'll try it out tomorrow

    I'm suprised someone actually understood my problem...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. User defined 2-D array problem
    By Turtal in forum C Programming
    Replies: 5
    Last Post: 12-15-2007, 12:23 AM
  3. Stl lists and user defined types
    By figa in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2005, 11:09 AM
  4. Help! User defined functions
    By AdrenalinFlow in forum C Programming
    Replies: 3
    Last Post: 02-22-2003, 06:36 PM
  5. User Defined save file (help)
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 04-16-2002, 11:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21