Hey folks, I'm writing a class called Set that can put in objects (ints, chars, whatever) into a dynamically allocated array. I'm having issues with two functions, union() (which concatenates two arrays of the Set class) and intersection() (which puts two arrays together of the Set class at a point where they have data in common).
I can compile, but when I run the code I get a memory map and some other stuff, so I'm assuming I'm not using dynamic arrays correctly. Any help would be great.
template<typename T>
void Set<T>::unionize(Set<T> arrayTwo)
{
int tempSize;
int newSize;
tempSize = size;
newSize = size + arrayTwo.size;
temp = array;
array = new T[newSize];
for (int i = 0; i < tempSize; i++)
{
array[i] = temp[i];
}
for (int j = tempSize; j < newSize; j++)
{
array[j] = arrayTwo.array[j];
}
delete [] arrayTwo.array;
}
template<typename T>
void Set<T>::intersection(Set<T> arrayTwo)
{
int intersect;
int tempSize = size;
int newSize = size + arrayTwo.size;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < arrayTwo.size; j++)
{
if (array[i] == arrayTwo.array[j])
{
intersect = i;
}
}
}
temp = array;
array = new T[newSize];
for (int a = 0; a < intersect; a++)
{
array[a] = temp[a];
}
for (int b = intersect; b < newSize; b++)
{
array[b] = arrayTwo.array[b];
}
}