Const_iterator implementation

Hi everybody, I need some help for the creation of a const_iterator. I've made a 'stack' class where I use a dynamic array for the data of the stack and I need a const_iterator in order to see all the elements of the stack form the first (at the end of the array) to the last (first element of the array).

Here's the declaration of the class:

1
2
3
4
5
6
7
8
9
template <typename S, typename T>
class stack {

	T *dyn_array;  
	T *first;  ///< pointer to the first element of the stack
	int _size;  


///methods 


and the const_iterator is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class const_iterator {

		friend class stack;

		const T *ptr; ///< Pointer
		int k; ///< Index

		explicit const_iterator(const T *i, int l) : ptr(i), k(l) {}

	public:

		typedef std::forward_iterator_tag iterator_category;
		typedef T                         value_type;
		typedef ptrdiff_t                 difference_type;
		typedef const T*                  pointer;
		typedef const T&                  reference;


		const_iterator() : ptr(0), k(0) {}



		const_iterator(const const_iterator &other) : ptr(other.ptr), k(other.k) {}



		const_iterator&operator=(const const_iterator &other) {
			ptr = other.ptr;
			k = other.k;
			return *this;
		}


		~const_iterator() {}



		reference operator*() const {
			return *ptr;
		}



		pointer operator->() const {
			return &ptr;
		}



		const_iterator& operator++() {
			k = k--;
			ptr = *(dyn_array[k]);
			return *this;
		}



		const_iterator operator++(int) {
			const_iterator tmp(*this);
			k = k--;
			ptr = dyn_array[k];
			return tmp;
		}



		bool operator==(const const_iterator &other) const {
			return (ptr == other.ptr);
		}



		bool operator!=(const const_iterator &other) const {
			//return (ptr != other.ptr);		
			return !(*this == other);
		}


	};


When I try to compile the code there's an error:

In file included from main.cpp:1:0:
stack.h: In instantiation of ‘stack<S, T>::const_iterator& stack<S, T>::const_iterator::operator++() [with S = std::basic_string<char>; T = char]’:
main.cpp:39:55: required from here
stack.h:25:5: error: invalid use of non-static data member ‘stack<std::basic_string<char>, char>::dyn_array’
stack.h:284:4: error: from this location


The line 284 is the 'ptr = *(dyn_array[k]);' in the increment of the pointer.
I really don't know how to solve this problem...
Thank you and sorry if i've made english mistakes :)
Last edited on
In your class const_iterator you don't have a dyn_array. You don't have a reference to stack at all.
The 'const_iterator' class is inside the 'stack' class. Isn't this enough? And how can i solve the problem, do I have to copy the array in the 'const_iterator' class? Thank you again.
The 'const_iterator' class is inside the 'stack' class. Isn't this enough?
No. You need access to the actual object.

And how can i solve the problem, do I have to copy the array in the 'const_iterator' class?
No. ptr is supposed to point to stack. Currently it's rather useless.

It can be done like so:
1
2
3
4
5
6
7
8
template <typename S, typename T>
class stack {
...
  const_iterator begin() const
  {
    return const_iterator(this); // k is set to 0 (or _size?) in this case
  }
...


By the way: k = k--; should look like this: k--; or --k;
Check whether k is within the valid range. You should at least have an assert if not.
Ok now I understand! Thank you very much :)
Topic archived. No new replies allowed.