Basically I want duplicate to resize my array. The function next is suppose to search for next available location in the array. Once next hits 10 which is the max capacity I want it to call duplicate. Basically call duplicate if the array has been found to be full within the next function.
The problem is that when I get the loop to 10 numbers it fails on the 11th. After the 10th element is in, it goes into the duplicate function, then it goes to the pointer, then through the array, and then it just keeps looping, so when I use my function to see the total number of elements it still says 10, which means maxsize
is not changing. I do not understand why?
void ProgramOne<Type>::Next(Type & y)
{
if (!IsFull())
{
if (count < maxsize)
{
bag[count] = y;
count++;
}
}
else
{
Duplicate();
}
}
I am trying to call this
void ProgramOne<Type>::Duplicate()
{
int *bagB = new int[maxsize * 2];
for (int i = 0; i < maxsize; i++)
{
bagB[i] = bag[i];
}
delete[] bag;
bag = bagB;
}
Your Duplicate()
function fails to update maxsize
after it is done copying the original elements. Before it returns, it should update the maxsize
to what it had allocated for the new bag
.
delete[] bag;
bag = bagB;
maxsize *= 2;