So, I have created a derived class template called unorderedSet that uses a pointer member variable to hold the location of an array on the heap (using new).
I am trying to overload the arithmetic operators to find both the intersection and union of two different unorderedSet objects, and the code I have written so far (with the help of another user on this site) manages that. However, upon running the program I get the error in the title. I will post the relevant parts of my code below.
template <class elemType>
class unorderedSet: public unorderedArrayListType<elemType>
{
public:
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void replaceAt(int location, const elemType& repItem);
const unorderedSet<elemType> operator+(const unorderedSet<elemType>&);
// Function to overload the binary operator + to find the union of a pair/group of sets
// Postcondition: Finds the union of the sets
const unorderedSet<elemType> operator-(const unorderedSet<elemType>&);
// Function to overload the binary operator - to find the intersection of a pair/group of sets
// Postcondition: Finds the intersection of the sets
unorderedSet(int size = 100);
unorderedSet(const unorderedSet<elemType>& otherSet);
~unorderedSet();
protected:
elemType *set;
int length;
int maxSize;
};
template <class elemType>
const unorderedSet<elemType> unorderedSet<elemType>::operator+(const unorderedSet<elemType>& otherSet)
{
unorderedSet<elemType> unSet(this->length + otherSet.length); // Initializes new set to hold values of the union set
for (int i = 0; i < this->length; i++)
unSet.insertEnd(this->list[i]); // Assigns all values of the activating operand to the union set using insertEnd
for (int i = 0; i < otherSet.length; i++)
unSet.insertEnd(otherSet.list[i]); // Calls insertEnd() to both check for duplicate values and add unique values to the union of the sets
return unSet; // Should return the union set, but dumps the core at the moment
} // end operator overload
template <class elemType>
unorderedSet<elemType>::unorderedSet(int size) : unorderedArrayListType<elemType>(size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating an array of the size 100. " << endl;
this->maxSize = 100;
}
else
this->maxSize = size;
this->length = 0;
set = new elemType[this->maxSize];
}
template <class elemType>
unorderedSet<elemType>::~unorderedSet()
{
delete [] set;
}
template <class elemType>
unorderedSet<elemType>::unorderedSet(const unorderedSet<elemType>& otherSet)
{
this->maxSize = otherSet.maxSize;
this->length = otherSet.length;
set = new elemType[this->maxSize];
for (int j = 0; j < length; j++)
set[j] = otherSet.set[j];
}
The following code comes from my test client program.
int main()
{
int intArr1[6] = {0, 1, 2, 3, 4, 5};
unorderedSet<int> testIntSet1;
for (int i = 0; i < (sizeof(intArr1) / sizeof(intArr1[0])); i++)
testIntSet1.insertEnd(intArr1[i]);
// Some more code before the function call
int intArr2[6] = {0, 1, 3, 6, 7, 9};
unorderedSet<int> testIntSet2, testIntSet3;
for (int i = 0; i < (sizeof(intArr2) / sizeof(intArr2[0])); i++)
testIntSet2.insertEnd(intArr2[i]);
testIntSet3 = testIntSet1 + testIntSet2;
// Some more code
}
I also have an assignment operator overload function in the base class of this class's base class. The code is as follows
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator= (const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete [] list;
this->maxSize = otherList.maxSize;
this->length = otherList.length;
list = new elemType[this->maxSize];
for (int i = 0; i < this->length; i++)
list[i] = otherList.list[i];
}
return *this;
}
I have attempted to create another version of this in my unorderedSet class, but when I run it the testIntSet3 variable outputs nothing (not even some random garbage). I decided to remove it since the code seemed to at least function properly before.