Creating iterator for my stack

Mar 11, 2012 at 8:36pm
I'm trying to create some iterators for my class stack so that I can accomplish something like this when trying to loop through my stack:

 
 for(StackIterator<T> i = begin(); i != end(); ++i)


where end is the 0th element of my stack (in an array), and begin is the element at the "top" of my stack (last element with info in array).

The problem I'm having is understanding how to create the begin() and end() function. I know I should be returning a iterator pointing to the last element for the begin() function and the first element for the end() function, but how would I create these functions? This is what I'm trying but I can't figure out how to make it work:

1
2
3
4
5
6
template <typename T>
StackIterator<T> Stack<T>::begin() const{
	 T *topOfStack = items[top];

	return topOfStack;
}

1
2
3
4
5
6
template <typename T>
StackIterator<T> Stack<T>::end() const{
	T *bottomOfStack = items[0];

	return bottomOfStack;
}
Mar 12, 2012 at 4:08am
you can't return a 'T' pointer from a function that return a 'StackIterator<T>' templated class.
Topic archived. No new replies allowed.