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; }
2 ответов
+ 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?
+ 1
Thanks. I found the mistake. It was in the constructor for the Automobile class.