【问题标题】:No matching function for call to push_back对 push_back 的调用没有匹配的函数
【发布时间】:2014-11-17 06:23:54
【问题描述】:

我想在控制台中打印一棵二叉树,这是我编写的代码,但我无法弄清楚这个错误。有人可以帮我吗?错误是 No matching function for call to push_back。

void getVerticalOrder(Node* root, int hd,std::map<int,std::vector<int> > &m)
    {
        // Base case
        if (node == NULL)
            return;
        // Store current node in map 'm'
        m[hd].push_back(root->payload); //Error in this line

        // Store nodes in left subtree
        getVerticalOrder(root->left, hd-1, m);

        // Store nodes in right subtree
        getVerticalOrder(root->right, hd+1, m);
    }

【问题讨论】:

  • payloadint 吗? (作为一般提示,您应该在 StackOverflow 上发布整个错误消息...)
  • 不,它是字符串@TonyD
  • 您不能在std::vector&lt;int&gt; 中存储字符串。你需要一个std::vector&lt;std::string&gt;
  • 那么你为什么期望能够将string 推送到std::vector&lt;int&gt; 中呢?如果您希望string 包含int 的文本表示,那么.push_back(std::stoi(root-&gt;payload)); - 请参阅here。如果它可能不是有效的int,并且您不希望抛出异常,请遵循 Gypati 的建议。
  • 哦,是的,这是一个非常愚蠢的错误。谢谢大家。

标签: c++ vector data-structures map binary-tree


【解决方案1】:

因为payload 是你需要的字符串:

std::map<int,std::vector<std::string> >
//                         ~~~ vector of std::string

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2016-08-09
    相关资源
    最近更新 更多