Issue to erase from vector | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Issue to erase from vector

Hi Below code is working fine but if I uncomment the line related to erase from main function, it throws run time error. The line causing problem is below one : v.erase(v.begin()); Could anyone please suggest on this ? https://code.sololearn.com/ciiWOqsK1E54/?ref=app

1st Mar 2022, 8:02 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 1
Ketan Lalcheta I did some more digging. The the problem is that when the move constructor or operator is defined. The copy operator is implicitly defined as deleted. Unless you explicitly define the copy operator yourself. Somewhere inside the erase method, copy operator is apparently used. Adding the following copy operator fixes that. Test& operator=(const Test& t) { return *this; }
2nd Mar 2022, 8:24 AM
Mustafa A
Mustafa A - avatar
+ 3
The erase method remove the element and move the following elements backwards. Otherwise, you would have holes in your vector. But you have overloaded (to be exact, implicitly declared) the move operations in Test. That's why it's failing. It can't move the elements.
1st Mar 2022, 10:05 PM
Mustafa A
Mustafa A - avatar
+ 1
Thanks Mustafa A It worked when I provided copy assignment operator... As soon as move assignment operator is added , same is called. Thanks again for your help...
2nd Mar 2022, 11:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Yeah got it.... Can I erase even with move constructor ? I mean is it possible to have move constructor and still use erase by changing the code for move constructor ? My move constructor is not proper or what ?
2nd Mar 2022, 3:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar