首页 > 解决方案 > 错误:没有匹配的成员函数调用“推送”

问题描述

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;
   }
};```

标签: c++treestack

解决方案


s1is of type stack<TreeNode *>,这意味着它只能push是类型的值TreeNode *

But rootis 的类型Node* ,与 不同TreeNode *

即使这些类型是相似的(并且可能是可转换的),因为函数的参数没有被识别为TreeNode *类型,所以函数没有被识别为一个整体,因此错误!

如果您注意错误消息,则表示该push函数的参数应该是const value_type&value_type&&。这意味着要么const TreeNode *&TreeNode *&&

root不属于这些类型!


推荐阅读