2

Thanks to the help I received in this post:

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.

Community
  • 1
  • 1
Peter Stewart
  • 2,857
  • 6
  • 28
  • 30

3 Answers3

9

They are member functions:

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

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

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

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

s.top();
s.pop();
Niki Yoshiuchi
  • 16,883
  • 1
  • 35
  • 44
1

top() is a member function in std::stack not a member variable. So you need the parenthesis while calling top

Naveen
  • 74,600
  • 47
  • 176
  • 233