I'm trying to reverse a stack (S) using two other stacks (S1 and S2). This is the code I'm trying:
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> S, S1, S2;
S.push(1), S.push(2), S.push(3);
cout << "The top element of S is: " << S.top() << endl;
while (!S.empty()) {
S1.push(S.pop());
}
while (!S1.empty())
S2.push(S1.pop());
while (!S2.empty())
S.push(S2.pop());
cout << "The top element of S is now: " << S.top() << endl;
return 0;
}
This is the error I'm getting (x3) for each time I call pop from inside push.
- stack.cc:14:11: error: reference to type 'const value_type' (aka 'const int') could not bind to an rvalue of type 'void' S1.push(S.pop());
I've tried assigning the popped value to a variable and then calling push with that variable, but it was unsuccessful also.
Any help would be appreciated!