Arnkrishn
Arnkrishn

Reputation: 30424

Error while inserting pointer to a vector

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

Upvotes: 1

Views: 978

Answers (3)

mocj
mocj

Reputation: 1486

You need to push a std::pair.

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

Upvotes: 7

jgottula
jgottula

Reputation: 1366

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.

Upvotes: 2

EdH
EdH

Reputation: 3293

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

Upvotes: 1

Related Questions