0

I'm writing a program where I'm supposed to add and delete cars from a Rent-a-Car company. My code works fine, but I'm supposed to overload the += operator to add a car to a dynamically allocated array of objects. Every time I add a car, the array size is required to increase by one. I must not use vectors.

If I don't do any copying, set the dynamic array size to a large number and just do this in operator +=, for example:

cars[numcars] = obj;
numcars++;
return *this;

the code works fine and I'm getting the output; however, if I try to:

  • create new object array[size+1]
  • copy old array to new array;
  • add new object to new array;
  • delete old array;
  • point original array pointer to new array;

It doesn't work and the program crashes after the first input. I've tried many variations but I can't seem to get where I'm going wrong. I have two classes, one Automobile and one RentACar (where I have the array of objects). In the car class there are 2 constructors (out of which one is copy constructor, operators =, == and << are overloaded and they work fine.


This is my latest attempt at overloading the += operator. numcars is set to 0

 RentACar& operator+=(Automobile &obj){
    Automobile *temp = new Automobile[numcars+1];
    for (int i = 0; i < numcars+1; i++){
        temp[i] = cars[i];
    }
    numcars++;
    temp[numcars] = obj;
    delete [] cars;
    cars = temp;
    return *this;
}

and this is the part in main()

for (int i=0;i<n;i++)
{
    char marka[100];
    int regisracija[5];
    int maximumBrzina;

    cin>>marka;

    for (int i=0;i<5;i++)
        cin>>regisracija[i];

    cin>>maximumBrzina;
    Automobile nov=Automobile(marka,regisracija,maximumBrzina);

    //add car
    agencija+=nov;
    }

The function is expected to make the "cars" array larger by one, and add the new object at the end. Instead, the program crashes after input of maximumBrzina.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
kasa ssg
  • 9
  • 1
  • 2
    What's `agencija`? Where's the definition of `RentACar`? – Blaze Aug 21 '19 at 11:51
  • 2
    `for (int i = 0; i < numcars+1; i++) { temp[i] = cars[i]; }` is incorrect. You have `numcars` entries used in `cars` (at positions `0`..`numcars-1`) but you attempt to copy `numcars+1` items (positions `0`..`numcars`). This can be the source of the crash because the memory at `cars[numcars]` contains garbage data that the copy constructor tries to interpret and access. If the objects of class `Automobile` contain pointers (directly or indirectly), the crash is guaranteed. It should be `i = 0; i < numcars; i ++`. – axiac Aug 21 '19 at 11:54
  • 2
    When I see the requirement "I must not use vectors." my first thought goes to filling in `namespace not_std { template class not_vector{ ... }; }` with as many of the members of `std::vector` as seem appropriate. – Caleth Aug 21 '19 at 11:57
  • https://pastebin.com/wZNNHqqQ Here is a link to the whole program, with the code that works inside the operator+=. – kasa ssg Aug 21 '19 at 12:00
  • @Caleth I'd go even one step further and use a `std::array` as backend :P the only thing left to reimplement from scratch is resizing, which becomes almost trivial – 463035818_is_not_an_ai Aug 21 '19 at 12:12
  • @formerlyknownas_463035818 if you limit yourself to power-of-2 capacities, that's a 32(64) line `switch`. Although it probably has to be `not_std::not_array` – Caleth Aug 21 '19 at 12:14

1 Answers1

2

You cannot overload operators for basic types or, in this case, pointers.

If you cannot use std::vector or any standard container, I think you should create your own.
Create a class that wraps your array. Then redefine operator+=() for this class in the way that it appends a new object at the end of the array member.

Fareanor
  • 5,900
  • 2
  • 11
  • 37