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:
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]; } };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; }
arrayfield is not initialized. - user194374arrayis 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