2

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);

Bhavya Arora
  • 57
  • 1
  • 1
  • 7
  • 1
    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! – Ulrich Eckhardt Mar 18 '15 at 19:10
  • Thank you for your comment @UlrichEckhardt ...can you explain me how are the objects being created or Constructor being called in the answers below. I only know that constructor is called upon creation with 'new' keyword. – Bhavya Arora Mar 18 '15 at 19:35
  • Actually, that explanation would be kind-of lengthy. However, knowing that not only `new` creates objects, you should now see plenty of examples in any tutorial or text about C++, where objects are created without `new`. – Ulrich Eckhardt Mar 19 '15 at 06:58

3 Answers3

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.

Rosme
  • 1,019
  • 10
  • 23
  • 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! – Bhavya Arora Mar 18 '15 at 19:24
  • 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. – Rosme Mar 18 '15 at 19:28
1

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());
Praetorian
  • 100,267
  • 15
  • 224
  • 307
  • 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 Mar 18 '15 at 19:32
  • 1
    @Bhavya Take a look at [this](https://stackoverflow.com/q/6500313/241631), it explains why dynamic allocation should not be the preferred approach in C++. But your question really indicates you should be reading [a book](https://stackoverflow.com/q/388242/241631) instead. – Praetorian Mar 18 '15 at 21:01
0

You can resolve this problem by simply deleting * and new from your first line.

This may be less efficient under some circumstance when compared to the other (also correct) suggestions, but it is a simpler change, and absolute maximum performance will rarely make a difference for this sort of thing.

I would like to point you to an earlier post of mine here for a little bit of background on the use of new in C++. To summarize that post, in C++ you don't call new to create an object, you just call the constructor. The use of new only changes how the memory is allocated for the object. Without new, the memory is allocated and managed automatically, but with new it is managed dynamically (using a heap) and must be handled a bit more carefully by the programmer.

Community
  • 1
  • 1
Brent Bradburn
  • 40,766
  • 12
  • 126
  • 136