error: no matching function for call to > ‘std::vector<MemberListEntry>::push_back and

15,832

Solution 1

You are trying to put a pointer into a vector that does not contain pointers. You have two possibility:

1- Change your vector<MemberListEntry> to vector<MemberListEntry*>

2- Change your first line to:

MemberListEntry object(id, port, memberNode->heartbeat, par->getcurrtime());

If you use the first one, make sure to call delete on the elements of the vector.

Solution 2

memberList contains MemberListEntrys and not MemberListEntry *s. Why do you want to dynamically allocate the MemberListEntry? Replace

MemberListEntry *object = new MemberListEntry(id, port, memberNode->heartbeat, par->getcurrtime());
memberNode->memberList.push_back(object);

with

memberNode->memberList.push_back(MemberListEntry(id, port, memberNode->heartbeat, par->getcurrtime()));

or better yet

memberNode->memberList.emplace_back(id, port, memberNode->heartbeat, par->getcurrtime());
Share:
15,832

Related videos on Youtube

Bhavya Arora
Author by

Bhavya Arora

Updated on June 04, 2022

Comments

  • Bhavya Arora
    Bhavya Arora almost 2 years

    Hi I am trying to insert an object of type MemberListEntry into a vector memberList containing the type . For this I am trying to use push_back function of vector, but it is giving me an error.

    MemberListEntry *object = new MemberListEntry(id, port, memberNode->heartbeat, par->getcurrtime());
    memberNode->memberList.push_back(object);
    

    MP1Node.cpp:118:41: error: no matching function for call to ‘std::vector::push_back(MemberListEntry*&)’
    memberNode->memberList.push_back(object);

    This is my vector

    vector<MemberListEntry> memberList;
    

    in class

    class Member {.........

    on changing to

    memberNode->memberList.push_back<MemberListEntry>(object);
    

    gives

    error: expected primary-expression before ‘>’ token
    memberNode->memberList.push_back(object);

    • Ulrich Eckhardt
      Ulrich Eckhardt about 9 years
      Just for the case that you have a background in Java, PHP, C#, don't use new in C++ like you would use it in those languages!
  • Bhavya Arora
    Bhavya Arora about 9 years
    Hi, thank you so much, the second one worked, the first I cannot do because I am not allowed to change the file in which the code resides. Can you please explain me how did it worked, I only studied that Constructor gets called on object creation and we create object with the use of 'new' operator. Sorry for limited knowledge. OBLIGED!
  • Rosme
    Rosme about 9 years
    This actually sound like you are lacking some basic knowledge of C++ and you should read on it. You only use new on pointers, indicated by the *. The constructor gets called everytime an object is constructed, pointer or not. Most of the time, you don't want to to use pointer unless really needed. Like I said, I suggest you go read on C++ as it is basic C++ knowledge.
  • Bhavya Arora
    Bhavya Arora about 9 years
    Hi, yes it works @Praetorian , but how does it works, the memberList is a vector of type MemberListEntry, then how do we place all this stuff directly, are we allowed to simply put it there? and in the first code MemberListEntry is a class so how does the constructor gets called without using new keyword?? Please explain.... Thank you so much for your help :) :)
  • Bhavya Arora
    Bhavya Arora about 9 years
    Thanks. I got it, I hope so :)
  • Praetorian
    Praetorian about 9 years
    @Bhavya Take a look at this, it explains why dynamic allocation should not be the preferred approach in C++. But your question really indicates you should be reading a book instead.