Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++: Can't find where error is in my code. Error = Core dumped Segmentation Fault. Need help ASAP! My code(below) works for the overloaded +operator,

C++: Can't find where error is in my code. Error = Core dumped "Segmentation Fault."

Need help ASAP! My code(below) works for the overloaded +operator, but the *operator and /operator causes a segmentation fault and I can't find where the issue is.

#include

#include

#include

using namespace std;

//template class//

template

class ValSet

{

private:

T *arr;

int arraySize;

int currVals;

public:

ValSet();

ValSet(const ValSet &obj);

ValSet& operator=(const ValSet& old);

~ValSet();

int size();

bool isEmpty();

bool contains(T);

void add(T);

void remove(T);

vector getAsVector();

ValSet operator+(const ValSet &a);

ValSet operator*(const ValSet &b);

ValSet operator/(const ValSet &c);

};

/************************************************************************

Default constructor

* initializes ptr data member 2 point to array of size 10

* initializes variable that stores the size of the array to 10

* initializes variable that stores # of values in set to zero.

*************************************************************************/

template

ValSet::ValSet()

{

arr = new T[10]; //initialize ptr

arraySize = 10; //initialize arraySize

currVals = 0; //initialize currVals

}

/************************************************************************

Copy constructor

* initializes ptr data member 2 point to array of same size as one being copied

* copies over the array values

* copies the values for the size of the array & # of vals in the set.

*************************************************************************/

template

ValSet::ValSet(const ValSet &obj)

{

arraySize = obj.arraySize; //copy vals for arraySize

arr = new T[arraySize]; //initialize ptr member to array of same size

currVals = 0; //initialize currVals

for(int i = 0; i < arraySize; i++) //copies over array values

{

arr[i] = obj.arr[i];

currVals++;

}

}

/************************************************************************

overloaded assignment operator=

* initializes ptr data member 2 point to an array of the same size as array being copied

* copies over the array values

* copies the vals for the size of the array and the number of values in the set

* returns a reference to the object pointed to by the "this" pointer.

************************************************************************/

template

ValSet& ValSet::operator=(const ValSet &rhs)

{

arraySize = rhs.arraySize; //set arraySize equal to size of array

currVals = rhs.currVals; //sets val for values in set

arr = new T[arraySize]; //initializes ptr to arr of same size

for(int i = 0; i < rhs.currVals; i++) //copy over values

{

arr[i] = rhs.arr[i];

}

delete[] arr;; //deallocates arr ptr

return *this; //returns reference to object

}

/************************************************************************

destructor

* deallocates the dynamically allocated array

************************************************************************/

template

ValSet::~ValSet()

{

delete[] arr;

arraySize = 0;

currVals = 0;

}

/************************************************************************

size() method

* returns the number of vals currently in the ValSet

************************************************************************/

template

int ValSet::size()

{

return currVals;

}

/************************************************************************

isEmpty() method

* returns true if the ValSet contains no values, otherwise returns False.

************************************************************************/

template

bool ValSet::isEmpty()

{

if(currVals == 0)

return true;

else

return false;

}

/************************************************************************

contains(T) method

* takes parameter of type T & returns true if T is in ValSet, & false otherwise

************************************************************************/

template

bool ValSet::contains(T val)

{

for(int i = 0; i < currVals; i++)

{

if(arr[i] == val)

return true;

}

return false;

}

/************************************************************************

add(T) method

* takes parameter of T & adds to ValSet if T is not already in ValSet

* If array is currently full, size of array is increased by:

* allocating a new array 2x as big,

* copying contents of old array to the new,

* redirecting the data member ptr to the new array &

* deallocating the old array.

************************************************************************/

template

void ValSet::add(T val)

{

if(!contains(val)) //verify value is not part of arr

{

if(currVals < arraySize) //if array still has room

{

arr[currVals] = val; //add val to arr

currVals++; //increment counter

}

else //if the array is full

{

T *temp = new T[arraySize * 2]; //temp ptr equal to new arr size 2x as big

for(int i = 0; 1 < arraySize; i++) //loop to copy vals

{

temp[i] = arr[i];

}

delete[] arr; //deallocate arr memory

arr = new T[arraySize * 2]; //set arr ptr = to new arr w/ 2x size

arr = temp; //set arr equal to temp arr w/ union vals

arraySize = arraySize * 2; //initialize arraySize

arr[currVals] = val; //add val to array

currVals++; //increment counter

}

}

}

/************************************************************************

remove(T) method

* takes a parameter of type T & if value is in ValSet, removes it

* Removal = by shifting over all of the subsequent elements of the array

************************************************************************/

template

void ValSet::remove(T val)

{

if(contains(val)) //verify if arr contains val

{

for(int n = 0; n < arraySize; n++) //loop to find values

{

if(arr[n] == val) //find array val that = val

{

arr[n] = arr[currVals - 1]; //shift over elements of arr

currVals--; //decrement counter

return;

}

}

}

}

/************************************************************************

getAsVector() method

* returns vector of T that contains all values in the ValSet & only those vals

************************************************************************/

template

vector ValSet::getAsVector()

{

vector vectVals; //return vector

for(int i=0; i < currVals; i++) //loop for values

{

vectVals.push_back(arr[i]); //add values to vector

}

return vectVals; //return vector

}

/************************************************************************

overloaded +operator

* returns a new ValSet that is the union of its two operands

* (contains all ond only those values that were in either ValSet).

***********************************************************************/

template

ValSet ValSet::operator+(const ValSet &s1)

{

ValSet newSet; //return set

for(int j=0; j < arraySize; j++) //copy original set to newSet

{

newSet.add(arr[j]);

}

for(int i = 0; i < s1.arraySize; i++) //loop for s1

{

if(!newSet.contains(s1.arr[i])) //if newSet does not contain s1 val

newSet.add(s1.arr[i]); //add value to newSet

}

return newSet; //return union

}

/************************************************************************

overloaded assignment operator*

* returns new ValSet that is the intersection of its two operands

* (contains all & only those values that were in both ValSets)

*************************************************************************/

template

ValSet ValSet::operator*(const ValSet &s1)

{

ValSet newSet; //return set

for(int j=0; j < currVals; j++) //loop of original set

{

for(int i=0; i< s1.currVals; i++) //loop for s1

{

if(arr[j] == s1.arr[j]) //find vals that are the sam

{

newSet.add(s1.arr[i]); //add vals to newSet

}

}

}

return newSet; //return intersection

}

/************************************************************************

overloaded assignment operator/

* returns new ValSet that is the symmetric difference of its two operands

* (contains all & only those vals that were in 1 ValSet or Other, but NOT both)

************************************************************************/

template

ValSet ValSet::operator/(const ValSet &s1)

{

ValSet unionSet; //union

ValSet newSet1;

for(int j=0; j < arraySize; j++) //copy original set to newSet1

{

newSet1.add(arr[j]);

}

unionSet = newSet1 + s1; //make set = union

ValSet intersectSet; //intersect

for(int j=0; j < currVals; j++) //loop of original set

{

for(int i=0; i< s1.currVals; i++) //loop for intersectSet

{

if(arr[j] == s1.arr[j]) //find vals that are the sam

{

intersectSet.add(s1.arr[i]); //add vals to intersectSet

}

}

//create set difference///

for(int i = 0; i < intersectSet.currVals; i++) //loop for intersect

{

for(int j=0; j < unionSet.currVals; j++) //loop for union values

{

if(intersectSet.arr[i] == unionSet.arr[j]) //compare intersect vals w/union

unionSet.remove(arr[i]); //remove if they match (set has difference)

}

}

ValSet newSet = unionSet; //set for return value, equals set diff

return newSet;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sql All In One For Dummies 7 Books In One

Authors: Allen G Taylor ,Richard Blum

4th Edition

1394242298, 978-1394242290

More Books

Students also viewed these Databases questions

Question

Enhance the basic quality of your voice.

Answered: 1 week ago

Question

Describe the features of and process used by a writing team.

Answered: 1 week ago