473,769 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem overloading the ++ operator of an iterator

20 New Member
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):


Expand|Select|Wrap|Line Numbers
  1.  
  2. template<typename T> class Row<T> : public std::vector<T> {};
  3.  
  4. template<typename T> class Grid<T> : public std::vector<Row<T> >
  5. {
  6. public:
  7.  
  8.        class iterator;
  9.  
  10.         iterator
  11.         begin()
  12.         {
  13.             typename std::vector<Row<T> >::iterator it;
  14.             it = this->std::vector<Row<T> >::begin();
  15.             typename Row<T>::iterator ri;
  16.             ri = it->begin();
  17.             iterator ret(it,ri);
  18.             return ret;
  19.         }
  20.  
  21.  
  22.         iterator
  23.         end()
  24.         {
  25.             typename std::vector<Row<T> >::iterator it;
  26.             it = this->std::vector<Row<T> >::end()-1; 
  27.             typename Row<T>::iterator ri;
  28.             ri = it->end();
  29.             iterator ret(it,ri);
  30.             return ret;
  31.         }
  32. };
  33.  
  34. template<typename T>
  35.     class Grid<T>::iterator : public Row<T>::iterator
  36.         {
  37.         private:
  38.             typedef typename std::vector<Row<T> >::iterator row_iterator;
  39.             typedef typename Row<T>::iterator cell_iterator;
  40.  
  41.             row_iterator row_iter;
  42.             cell_iterator cell_iter;
  43.  
  44.         public:
  45.             iterator(){}
  46.             iterator(row_iterator ri, cell_iterator ci) : row_iter(ri), cell_iter(ci) {}
  47.             iterator(const iterator& rhs) : row_iter(rhs.row_iter), cell_iter(rhs.cell_iter) {}
  48.  
  49.             iterator
  50.             operator=(const iterator& rhs)
  51.             {
  52.                 if(this = &rhs) return *this;
  53.  
  54.                 cell_iter = rhs.cell_iter;
  55.                 row_iter = rhs.row_iter;
  56.                 return *this;
  57.             }
  58.  
  59.             T&
  60.             operator*()
  61.             {
  62.                 return *cell_iter;
  63.             }
  64.  
  65.  
  66.             iterator&
  67.             operator++() //prefix   //this is where the problem is!
  68.             {
  69.                 cell_iter++;
  70.                 if (cell_iter == row_iter->end())
  71.                 {
  72.                     std::cout << "end of line" << std::endl;
  73.                     ++row_iter;
  74.                     cell_iter == row_iter->begin();
  75.                 }
  76.                 return *this;
  77.             }
  78.  
  79.  
  80.             bool
  81.             operator==(const iterator& rhs)
  82.             {
  83.                 return (cell_iter == rhs.cell_iter && row_iter == rhs.row_iter);
  84.             }
  85.  
  86.             bool
  87.             operator!=(const iterator& rhs)
  88.             {
  89.                 return (!*this == rhs);
  90.             }
  91.  
  92.  
  93.             void
  94.             test()
  95.             {
  96.                 cell_iter = row_iter->begin();
  97.                 while (cell_iter != row_iter->end()) std::cout << *cell_iter++;
  98.                 ++row_iter;
  99.                 cell_iter = row_iter->begin();
  100.                 std::cout << std::endl;
  101.                 while (cell_iter != row_iter->end()) std::cout << *cell_iter++;
  102.                 std::cout << std::endl << std::endl;
  103.             }
  104.         };
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.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. #include "Grid.h"
  4.  
  5. int main()
  6. {
  7.    Grid<int> grid(3,3);
  8.  
  9.     for (int i=0; i != 3; ++i)
  10.     {
  11.         for (int j=0; j!= 3; ++j)
  12.         {
  13.             grid[i][j]=i*3 + j + 1;
  14.         }
  15.     }
  16.  
  17.    std::cout << *(grid.begin()) << std::endl;  
  18.    std::cout << *(--grid.end()) << std::endl; 
  19.  
  20.    Grid<int>::iterator iter3 = grid.begin();  
  21.    iter3.test();                                 // this behaves as expected!
  22.  
  23.    Grid<int>::iterator iter = grid.begin();
  24.  
  25.     for (int i= 0; i!=15; ++i)
  26.     {
  27.         std::cout << *iter << "  ";  // this does not!
  28.         ++iter;
  29.     }
  30. }
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
Oct 22 '07 #1
4 2007
weaknessforcats
9,208 Recognized Expert Moderator Expert
I think on line 75 that you need to return cell_iter that you have been incrementing rather than this.
Oct 22 '07 #2
mkborregaard
20 New Member
I think on line 75 that you need to return cell_iter that you have been incrementing rather than this.
Thanks, but I do not see how that could work?
There is an implicit this->cell_iter++ in line 68. My iterator contains two other iterators:
Expand|Select|Wrap|Line Numbers
  1. Grid<T>::iterator : public Row<T>::iterator
  2. {
  3. typename std::vector<Row<T> >::iterator row_iter;
  4. typename Row<T>::iterator cell_iter;
  5. //...
  6. };
row_iter points at a row in the Grid, and cell_iter moves in that row. My intention with the increment operator is that when cell_iter reaches the end of its row, row_iter should point to the next row, and cell_iter to the first element of that row.
Oct 22 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Sorry. I was off base on my comment.

However, what about:
++row_iter;
cell_iter == row_iter->begin();
When row_iter == row_iter.end(), cell_iter is set back to row_iter.begin( ).

Maybe that should be:
Expand|Select|Wrap|Line Numbers
  1. if (++row_iter == row_iter.end()
  2. {
  3.     cell_iter = row_iter.end();
  4. }
  5. else
  6. {
  7.      cell_iter = row_iter.begin();
  8. }
  9. return *this;
  10.  
Oct 22 '07 #4
mkborregaard
20 New Member
Yes!
Finally it works :) thanks!
Michael
Oct 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1772
by: Grant Austin | last post by:
Hi all, I'm trying to overload the '==' operator for a class, Page, that I wrote so that I can use the STL List 'find' algorithm. I'm having difficulties getting a definition that g++ likes and that satisfies the requirements for STL. The only criteria I care about for equivalency are that the Page.page properties are equal. Below I'm including all of the errors and the two pertinent files. I apologize for including so much...I'm just...
6
12546
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
6
1791
by: woosu | last post by:
Hello ladies and gentlemen. I have a relatively simple problem that I've been unable to solve. In the interest of learning C++, I've decided to write a simple game. The basis for the game is 8x8 board (think chess). I decided to implement iterators so it'd be easy to analyze the board position etc. Basically I have the following hierachy:
1
501
by: tim | last post by:
I am trying to convert numeric data held in a vector to a vector of string. The method is listed below. My compiler (VC6) reports the error "error C2227: left of '->data' must point to class/struct/union" I'd appreciate any help on this because I am clueless about why the iterator isnt being implemented. If I change the type to a std::string
1
8120
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip My problem is that when I try to compile the library I got a list of linking error messages. I am not a specialist of porting C++ code to .Net. Does anyone has an idea on how to make this code compile in .Net ? Thanks, Joannes Vermorel
3
4653
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
17
2333
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks << endl;
3
2264
by: Gerhard Pfeiffer | last post by:
Hi, I'm trying to implement a data-structure and have an iterator for it. Now I've got a problem impleneting the operator+. I tried to isolate the problem: template<int DIM, typename Tclass data_structure { private: int data; public: class iterator {
14
2087
by: Frank | last post by:
Hello everyone, I am having trouble overloading the < operator for an assignment. I use a struct that contains information and I would like to sort this structure using STL sort with my own criteria of sorting. Basically, I would like to sort on visitor count of the Attraction structure. However, it never uses the < overloaded operator with my code. Handler.h:
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.