I'm having trouble with using std::stack to retrieve the values from a recursive function.

Posted by Peter Stewart on Stack Overflow See other posts from Stack Overflow or by Peter Stewart
Published on 2010-05-04T17:38:53Z Indexed on 2010/05/04 17:48 UTC
Read the original article Hit count: 363

Filed under:
|
|
|

Thanks to the help I received in this post:

http://stackoverflow.com/questions/2761918/how-do-i-use-this-in-a-member-function

I have a nice, concise recursive function to traverse a tree in postfix order:

void Node::postfix()
{
        if (left != __nullptr) { left->postfix(); } 
        if (right != __nullptr) { right->postfix(); } 
                cout<<cargo<<"\n"; 
        return;
};

Now I need to evaluate the values and operators as they are returned. My problem is how to retrieve

them. I tried the std::stack:

#include <stack> 
stack <char*> s;
void Node::postfix()
{
        if (left != __nullptr) { left->postfix(); } 
        if (right != __nullptr) { right->postfix(); } 
        s.push(cargo);
        return;
};

but when I tried to access it in main()

while (!s.empty())
{
    cout<<s.top<<"\n";
    s.pop;
}

I got the error:

'std::stack<_Ty>::top': function call missing argument list; use '&std::stack<_Ty>::top' to create

a pointer to member'

I'm stuck.

Another question to follow shortly.

© Stack Overflow or respective owner

Related posts about c++

Related posts about recursion