代码之家  ›  专栏  ›  技术社区  ›  Arnkrishn

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

  •  1
  • Arnkrishn  · 技术社区  · 15 年前

        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*> >]
    

    请帮助我排除错误消息的故障。

    干杯

    3 回复  |  直到 15 年前
        1
  •  7
  •   mocj    15 年前

    您需要推送std::pair。

    nodeCountList.push_back(std::make_pair(1,nodePtr));
    
        2
  •  2
  •   jgottula    15 年前

    您试图将两个参数传递给 nodeCountList.push_back ,它只接受一个参数。相反,首先创建一个 std::pair 里面有两个你想要的东西。然后,打电话 std::pair

        3
  •  1
  •   EdH    15 年前

    您是否尝试过先将“node”图灵化为一个类型,然后使用模板?也许这样会更好。