Hi,
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):
I have added all the code for consistency, but the problem is where I have indicated by my comment.
It should all compile without problems, but has an unwanted behaviour when i test it, e.g.
- #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;
-
}
-
}
when I conduct iter3.test() i get the behaviour i want - it writes
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