Hi all, for a class assignment we are to create a class (infixToPostfix) that will handle functions to store the infix, show the infix, convert the infix to postfix, and determine precedence of two operators. My coding was going smoothly until I tried coding the convertToPostfix function. I got the algorithm from an online source and tried to cater it to my program but I'm getting errors as it pertains to popping operands from the stack.
I get what the algorithm is supposed to do, my issue is that I'm getting errors while popping the temp char.
1 2 3 4
infixToPostfix.h:89: error: no matching function for call to `std::stack<char, std::deque<char, std::allocator<char> > >::pop(char&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_stack.h:206: note: candidates are: void std::stack<_Tp, _Sequence>::pop() [with _Tp = char, _Sequence = std::deque<char, std::allocator<char> >]
infixToPostfix.h:103: error: no matching function for call to `std::stack<char, std::deque<char, std::allocator<char> > >::pop(char&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_stack.h:206: note: candidates are: void std::stack<_Tp, _Sequence>::pop() [with _Tp = char, _Sequence = std::deque<char, std::allocator<char> >]
Am I defining the temp char properly? I'm getting more confused as I fiddle with this. From the error I'm thinking that stack.pop(temp); is illegal, or am I concatenating it improperly?
It seems you're not calling pop() properly. http://www.cplusplus.com/reference/stl/stack/pop/
pop() only removed the top element from the stack. If you need to also get this value, you need to first call top():
1 2 3 4
std::stack<int> s;
s.push(20);
int a=s.top();
s.pop(); //no parameters
Usually, std::vectors are more versatile than std::stacks, while keeping the same functionality with push_back() and pop_back().
You know what, I have no idea what I was thinking, but my function works like a charm by simply adding temp = stack.top(); before the pop call that was having errors. Calling stack.pop() followed by pfx += temp; then works fine. Thanks for the simple yet somewhat overlooked step!