我在使用 std::stack 从递归函数检索值时遇到问题
感谢我在这篇文章中收到的帮助:
我有一个漂亮、简洁的递归函数,可以按后缀顺序遍历树:
void Node::postfix()
{
if (left != __nullptr) { left->postfix(); }
if (right != __nullptr) { right->postfix(); }
cout<<cargo<<"\n";
return;
};
现在我需要在返回值和运算符时对其进行求值。我的问题是如何检索
它们。我尝试了 std::stack:
#include <stack>
stack <char*> s;
void Node::postfix()
{
if (left != __nullptr) { left->postfix(); }
if (right != __nullptr) { right->postfix(); }
s.push(cargo);
return;
};
但当我尝试在 main() 中访问它时,
while (!s.empty())
{
cout<<s.top<<"\n";
s.pop;
}
出现错误:
'std::stack<_Ty>::top': 函数调用缺少参数列表;使用 '&std::stack<_Ty>::top' 创建
指向成员的指针'
我陷入困境。
很快就会出现另一个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们是成员函数:
这就是错误显示“函数调用缺少参数列表”时的含义。参数列表(在本例中为空,因为该函数不带参数)和括号丢失。
They are member functions:
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.
top 和 pop 是函数,而不是成员变量。你应该写
top and pop are functions, not member variables. You should write
top()
是std::stack
中的成员函数,而不是成员变量。因此,在调用top
时需要括号top()
is a member function instd::stack
not a member variable. So you need the parenthesis while callingtop