c++ - 错误:没有匹配的成员函数调用“推送”
问题描述
Line 30: Char 11: error: no matching member function for call to 'push'
s1.push(root);
~~~^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_stack.h:233:7: note: candidate function not viable: no known conversion from 'Node *' to 'const std::stack<TreeNode *, std::deque<TreeNode *, std::allocator<TreeNode *>>>::value_type' (aka 'TreeNode *const') for 1st argument
push(const value_type& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_stack.h:238:7: note: candidate function not viable: no known conversion from 'Node *' to 'std::stack<TreeNode *, std::deque<TreeNode *, std::allocator<TreeNode *>>>::value_type' (aka 'TreeNode *') for 1st argument
push(value_type&& __x)
^
public:
vector<int> postorder(Node* root) {
vector<int> v;
if(root==NULL){
return v;
}
stack<TreeNode *> s1;
stack<TreeNode *> s2;
s1.push(root);
while(!s1.empty()){
TreeNode* f=s1.top();
s1.pop();
s2.push(f);
for(int i=0;i<f->children.size();i++){
if(f->children[i]){
s1.push(f->children[i]);
}
}
}
while(!s2.empty()){
v.push_back(s2.top()->val);
s2.pop()
}
return v;
}
};```
解决方案
s1
is of type stack<TreeNode *>
,这意味着它只能push
是类型的值TreeNode *
。
But root
is 的类型Node*
,与 不同TreeNode *
。
即使这些类型是相似的(并且可能是可转换的),因为函数的参数没有被识别为TreeNode *
类型,所以函数没有被识别为一个整体,因此错误!
如果您注意错误消息,则表示该push
函数的参数应该是const value_type&
或value_type&&
。这意味着要么const TreeNode *&
或TreeNode *&&
!
root
不属于这些类型!
推荐阅读
- docker - 在 arm32v7/ubuntu:20.04 docker 中运行 apt update 时出现 GPG 无效签名错误
- java - 在 Alexa Skill 的 Intent 处理程序中使用 java.awt.Desktop
- java - 在 Eclipse 中,在哪里为 Contextlistener 添加外部库?
- flutter - Flutter & Introduction Slider : 我们怎么知道这是我们的用户第一次打开open?
- javascript - 使用 saga 和 axios 处理上传进度
- python-3.x - python打印嵌套for循环的进度而不使用第三方包
- microsoft-graph-api - Microsoft Teams Graph API - 列出外部聊天的成员时,无法获取名称
- flutter - 为什么在 Flutter 中会发生 Facebook 登录错误?
- typeorm - MySQL typeORM中的复合主键
- python - 用他们的分数打印球员