Problem: I can not display the numbers that are in the array. I thought that in the value method I need to write return *array[current] , but I get:

error: indirection requires pointer operand ('int' invalid)

If I remove the "dereferer" return array[current] then I get garbage in the output

1359699216 32767 0 0 1359699224 32767 1359699252 32767 1359699268 32767

Inside the constructor, I “for experiment” inscribed a cycle, outputting an array — everything output is very good. But when I try to return a value through a function, I get garbage.

It is hard for me to get pointers and links, all the time I get confused in them. If it's not difficult, can you give an article where everything is available?

Implementation:

  1. ArrayIterator.cpp

     #include <iostream> class ArrayIterator { private: int *array; int current; int size; public: ArrayIterator(int *array, int size) { this->size = size; this->current = 0; } void next() { if ( over() ) { return; } current += 1; } bool over() { int last = size - 1; return current > last; } int operator[](int index) { return array[index]; } int value() { return array[current]; } }; 
  2. main.cpp:

     #include "ArrayIterator.cpp" int main() { int array[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; ArrayIterator seq(array, 10); for ( ; !seq.over(); seq.next() ) { std::cout << seq.value() << std::endl; } return 0; } 
  • 2
    In the constructor of the iterator, the array field is not initialized. - user194374
  • array is an array / pointer, array[current] is an element of an array, not a pointer to an element. A dereference is not necessary. See the previous comment for the cause of the error. - VladD

2 answers 2

In the class constructor, you forgot to initialize the array member

 ArrayIterator(int *array, int size) { this->size = size; this->current = 0; } 

The constructor may look like this.

 ArrayIterator( int *array, int size ) : array( array ), current( 0 ), size( size ) { } 

The next and over functions can be written more easily. for example

 void next() { if ( !over() ) ++current; } bool over() const { return !( current < size ); } 

    Sorry, but who will initialize the array field, the house manager?

    Next, look through Myers. Understand what the initialization lists are in the constructor and why you need them. Initializing the fields as you do is incorrect.

    • Apparently, a person used to program in Java . There is a record in the order of things. - user194374
    • @kff let it be programmed at least on honey buds - discovered a new language, read the best practices of application. What, Scott tried in vain, trampled on the clave? For such after all "clever men" the uncle works. - gbg
    • @gbg that I just learn) thanks for the criticism and yes, inattention is my misfortune. but as soon as I wrote back the question immediately and found a mistake, this is mysticism, until that time I was sitting and I can’t understand what is wrong - Volodymyr Samoilenko
    • @VladimirSamoilenko: You definitely need to look here . - VladD