I have a class with items pointing to an Airport object. The size of the dynamic array starts off at 10. When total is greater than the size of the array and I have read in data to fill up the array, I need to double the size of the array, copy the elements from the old array into the newly sized array and then delete the old array. The array will continue to read in data, then resize, and continue this until all the data from the file is read into the items array.
There are no errors that occur, but the problem is when I run the program, it crashes. I think my doubleSize function to resize the array might be the cause of this. Is there a way to fix this issue?
class AirportList
{
private:
Airport* items;
int total;
int maxElements;
int oldAmount;
public:
AirportList()
{
oldAmount = 10;
maxElements = 10;
items = new Aiport[maxElements];
// a file was opened to read in data before this,
// total equals the total amount of lines in the file
string cppstr;
for (int counter = 0; counter < total; counter++)
{
getline(infile, cppstr);
items[counter] = Airport(cppstr); // the Airport constructor
// parses each line to read in
// the data and sets each Airport
// object into the items array
if (total > maxElements && counter == maxElements)
doubleSize(items, maxElements, oldAmount);
}
infile.close();
}
void doubleSize(Airport*& orig, int maxElements, int oldAmount)
{
maxElements = maxElements * 2;
Aiport* resized = new Aiport[maxElements];
for (int i = 0; i < oldAmount; i++)
resized[i] = orig[i];
delete [] orig;
orig = resized;
oldAmount = maxElements;
}
};
oldAmountparameter todoubleSizesupposed to be a reference? If not then setting it on the last line does nothing. (ditto formaxElements. I think that HAS to be a ref,oldAmountnot so important.Airport ** items