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];
	}
}

Where do temp and array come from? I don't see any declarations. Based on usage, I suspect that array is a member of Set<T>, but where did temp come from? Make sure you declare it properly before you try to do anything with it.

Also, I don't think you're using the template correctly for your function arguments. But since I don't know how the rest of your code works, I can't say for sure. You may need to create a multiple-type template...

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.