+ 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
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;
}
+ 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.
+ 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...
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 ?