I am having trouble using std :: stack to extract values ​​from a recursive function

Thanks for the help I got in this post:

How do I use "his" 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 values ​​and operators as they are returned. My problem is how to get

them. I tried 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': call list of missing function arguments; use '& std :: stack <_Ty> :: top' to create

element pointer

I am stuck.

One more question that needs to be completed in the near future.

+2


a source to share


3 answers


They are member functions:

s.top()
s.pop()
     ^ need parentheses to call a function

      



That's what the error means when it says "list of missing function arguments". The argument list (which in this case is empty, since the function takes no parameters) and the parentheses are missing.

+9


a source


top and pop are functions, not member variables. You have to write



s.top();
s.pop();

      

+1


a source


top()

is a member function in a std::stack

non-member variable. So you need parentheses by callingtop

+1


a source







All Articles