I am trying to create a member function that is supposed to replicate a stack by using a list. So I have to print the elements of the list in reverse order. I have a private list declared in my header file as:
class miniStackLT {
private:
list<T> content;
I have the following code for that function:
template <typename T>
void miniStackLT<T>::PrintStack(){
list<T>::iterator iter;
iter = content.end();
iter--;
while (iter != content.end()) {
cout << *iter << " | ";
iter--;
}
}
When I run the program I check the size of the so list and it tells me the correct number but when I print it, it prints the correct elements then I get an error that says the list iterator is not decrementable.
What is causing this error? I have tried messing around with the starting and ending points of the loop and the iterator, but I keep getting the error.
Edit: I have also used
template <typename T>
void miniStackLT<T>::PrintStack(){
list<T>::iterator rit;
for (auto rit = content.rbegin(); rit != content.rend(); ++rit){
cout << *iter << " | ";
}
}
But I still get the error.