I have the weirdest problem, and I can not see what is going wrong.
I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very strange. The implementation looks like this (there is a lot of code, but I have only included it all for consistency. The problem is quite small and local):
Expand|Select|Wrap|Line Numbers
- template<typename T> class Row<T> : public std::vector<T> {};
- template<typename T> class Grid<T> : public std::vector<Row<T> >
- {
- public:
- class iterator;
- iterator
- begin()
- {
- typename std::vector<Row<T> >::iterator it;
- it = this->std::vector<Row<T> >::begin();
- typename Row<T>::iterator ri;
- ri = it->begin();
- iterator ret(it,ri);
- return ret;
- }
- iterator
- end()
- {
- typename std::vector<Row<T> >::iterator it;
- it = this->std::vector<Row<T> >::end()-1;
- typename Row<T>::iterator ri;
- ri = it->end();
- iterator ret(it,ri);
- return ret;
- }
- };
- template<typename T>
- class Grid<T>::iterator : public Row<T>::iterator
- {
- private:
- typedef typename std::vector<Row<T> >::iterator row_iterator;
- typedef typename Row<T>::iterator cell_iterator;
- row_iterator row_iter;
- cell_iterator cell_iter;
- public:
- iterator(){}
- iterator(row_iterator ri, cell_iterator ci) : row_iter(ri), cell_iter(ci) {}
- iterator(const iterator& rhs) : row_iter(rhs.row_iter), cell_iter(rhs.cell_iter) {}
- iterator
- operator=(const iterator& rhs)
- {
- if(this = &rhs) return *this;
- cell_iter = rhs.cell_iter;
- row_iter = rhs.row_iter;
- return *this;
- }
- T&
- operator*()
- {
- return *cell_iter;
- }
- iterator&
- operator++() //prefix //this is where the problem is!
- {
- cell_iter++;
- if (cell_iter == row_iter->end())
- {
- std::cout << "end of line" << std::endl;
- ++row_iter;
- cell_iter == row_iter->begin();
- }
- return *this;
- }
- bool
- operator==(const iterator& rhs)
- {
- return (cell_iter == rhs.cell_iter && row_iter == rhs.row_iter);
- }
- bool
- operator!=(const iterator& rhs)
- {
- return (!*this == rhs);
- }
- void
- test()
- {
- cell_iter = row_iter->begin();
- while (cell_iter != row_iter->end()) std::cout << *cell_iter++;
- ++row_iter;
- cell_iter = row_iter->begin();
- std::cout << std::endl;
- while (cell_iter != row_iter->end()) std::cout << *cell_iter++;
- std::cout << std::endl << std::endl;
- }
- };
It should all compile without problems, but has an unwanted behaviour when i test it, e.g.
Expand|Select|Wrap|Line Numbers
- #include<iostream>
- #include "Grid.h"
- int main()
- {
- Grid<int> grid(3,3);
- for (int i=0; i != 3; ++i)
- {
- for (int j=0; j!= 3; ++j)
- {
- grid[i][j]=i*3 + j + 1;
- }
- }
- std::cout << *(grid.begin()) << std::endl;
- std::cout << *(--grid.end()) << std::endl;
- Grid<int>::iterator iter3 = grid.begin();
- iter3.test(); // this behaves as expected!
- Grid<int>::iterator iter = grid.begin();
- for (int i= 0; i!=15; ++i)
- {
- std::cout << *iter << " "; // this does not!
- ++iter;
- }
- }
123
456
when i use the ++ operator below, it writes
1 2 3 end of line
## ## ## 4 5 6 end of line
## ## ## 7 8 9 end of line
where ## indicates some undefined number.
I would gladly appreciate any help you could give me, as I have now been looking at this problem for days.
Thanks