插入指向向量的指针时出错

发布于 2024-08-02 20:09:07 字数 916 浏览 2 评论 0原文

我有以下 CPP 代码片段和关联的错误消息:

代码片段

    struct node{
            char charVal;
            bool childNode;
            struct node *leftChild;
            struct node *rightChild;
    };
    vector<std::pair<int,struct node*> > nodeCountList;
    struct node *nodePtr = new struct node;
    nodeCountList.push_back(1,nodePtr); 

错误消息

error: no matching function for call to ‘std::vector<std::pair<int, node*>, std::allocator<std::pair<int, node*> > >::push_back(int&, node*&)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::pair<int, node*>, _Alloc = std::allocator<std::pair<int, node*> >]

请帮助我解决错误消息问题。

干杯

I have the following CPP code snippet and the associated error message:

Code snippet

    struct node{
            char charVal;
            bool childNode;
            struct node *leftChild;
            struct node *rightChild;
    };
    vector<std::pair<int,struct node*> > nodeCountList;
    struct node *nodePtr = new struct node;
    nodeCountList.push_back(1,nodePtr); 

Error message

error: no matching function for call to ‘std::vector<std::pair<int, node*>, std::allocator<std::pair<int, node*> > >::push_back(int&, node*&)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::pair<int, node*>, _Alloc = std::allocator<std::pair<int, node*> >]

Please help me troubleshoot the error message.

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-08-09 20:09:07

您需要推送 std::pair。

nodeCountList.push_back(std::make_pair(1,nodePtr));

You need to push a std::pair.

nodeCountList.push_back(std::make_pair(1,nodePtr));
紧拥背影 2024-08-09 20:09:07

您试图将两个参数传递给 nodeCountList.push_back,而它只接受一个参数。相反,首先创建一个 std::pair ,其中包含您想要的两个项目。然后,使用该 std::pair 作为参数调用 nodeCountList.push_back

You are trying to pass two arguments to nodeCountList.push_back, which only accepts one argument. Instead, first create a std::pair with the two items you want in it. Then, call nodeCountList.push_back with that std::pair as the argument.

淡淡の花香 2024-08-09 20:09:07

您是否尝试过先将“节点”转换为类型,然后使用您的模板?也许这样效果会更好。

Have you tried turining "node" into a type first, and then using your template? Maybe this will work better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文