can someone tell me what am i doing wrong in overloading this operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can someone tell me what am i doing wrong in overloading this operator

I am supposed to overload the += operator. It is supposed to insert a new Automobile object into the dynamic array cars.But the code keeps crashing. I overloaded the = operator and that part works just fine.(line 7) RentACar &operator+=(const Automobile &a1) { Automobile *tmp; tmp=new Automobile[num+1]; for(int i=0;i<num;i++) { tmp[i]=cars[i]; } tmp[num]=a1; delete [] cars; cars=tmp; num++; return *this; }

4th Apr 2021, 7:33 AM
Mihail
Mihail - avatar
2 Answers
+ 2
I don't necessarily see anything wrong with the operator, it looks just fine. I also checked with a dummy code containing minimal definitions for "Automobile" and "RentACar", which worked as well without errors. So unless I am missing something, I don't think whatever makes your code crash is due to this operator definition. Is it possible for you to link the entire code here?
4th Apr 2021, 7:56 AM
Shadow
Shadow - avatar
+ 1
Thanks. I found the mistake. It was in the constructor for the Automobile class.
4th Apr 2021, 3:18 PM
Mihail
Mihail - avatar