Help with overloading operator to do union, intersection, differences of sets

I'm quite new to C++, used to learn java, so I might ask something quite stupid here.
I'm kind of finishing coding most of them, but there are some errors that I can't figure out why it happens.
My +=(union), -=(differences), *=(intersection) operators work just fine when alone.
But if there is something like:
A += B;
C *= B;
in my main, the second one will cause error.
Also, I'm not sure why my code can't use multiple operators in same line:
e.g. A = B = C = D won't work
D = A * B + D will just give me result of A * B
Another thing is, if I want to accept something like A, which is need to be treat as an empty set, with size = 1, how should I create my constructor?

Here is my code:

#include "intset.h"


// ---------------------------------------------------------------------------
// Constructor
intset::intset() {
size = 10;
arrayPtr = new int[size];
set = new bool[size];
for (int i = 0; i < size; i++) {
arrayPtr[i] = NULL;
set[i] = false;
}
}
intset::intset(int a) {
size = a + 1;
arrayPtr = new int[size];
set = new bool[size];
for (int i = 0; i < size; i++) {
arrayPtr[i] = NULL;
set[i] = false;
}
if (a >= 0) {
arrayPtr[0] = a;
set[a] = true;
}
}

intset::intset(int a, int b) {
if (a > b) {
size = a + 1;
}
else {
size = b + 1;
}
arrayPtr = new int[size];
set = new bool[size];
for (int i = 0; i < size; i++) {
arrayPtr[i] = NULL;
set[i] = false;
}
if (a >= 0) {
arrayPtr[0] = a;
set[a] = true;
}
if (b >= 0) {
arrayPtr[1] = b;
set[b] = true;
}
}

// ---------------------------------------------------------------------------
// Destructor
// Destructor for class Array
intset::~intset() {
// delete[] arrayPtr;
}

// ------------------------------------------------------------------------
// operator+
// overloaded +: union of 2 sets, current object and parameter
intset intset::operator+(const intset& a) const {

intset sum;
int count = 0;
for (int i = 0; i < size; i++) {
sum.arrayPtr[count] = arrayPtr[i];
sum.set[arrayPtr[i]] = true;
count++;
}
sum.size = size;
for (int j = 0; j < a.size; j++) {
if (!sum.isInSet(a.arrayPtr[j])) {
sum.arrayPtr[count] = a.arrayPtr[j];
sum.set[a.arrayPtr[j]] = true;
count++;
sum.size++;
}
}
return sum;

}
// ---------------------------------------------------------------------------
// operator*
// overloaded -: intersection of 2 sets, current object and parameter

intset intset::operator*(const intset& a) const {

intset temp;
intset sec;
int count = 0;
for (int i = 0; i < size; i++) {
temp.arrayPtr[count] = arrayPtr[i];
temp.set[arrayPtr[i]] = true;
count++;
}
for (int j = 0; j < a.size; j++) {
if (temp.isInSet(a.arrayPtr[j])) {
sec.arrayPtr[count] = a.arrayPtr[j];
sec.set[a.arrayPtr[j]] = true;
count++;
}
}
return sec;

}


// ---------------------------------------------------------------------------
// operator-
// overloaded -: difference of 2 sets, current object and parameter

intset intset::operator-(const intset& a) const {

intset temp;
intset diff;
int count = 0;
for (int i = 0; i < size; i++) {
temp.arrayPtr[count] = arrayPtr[i];
temp.set[arrayPtr[i]] = true;
count++;
}
for (int j = 0; j < a.size; j++) {
if (!temp.isInSet(a.arrayPtr[j])) {
diff.arrayPtr[count] = a.arrayPtr[j];
diff.set[a.arrayPtr[j]] = true;
count++;
}
}
return diff;

}

// ---------------------------------------------------------------------------

(To be continue)
// operator+=
// overloaded +=: object = union of current object and parameter
intset& intset::operator+=(const intset& a) {


intset sum;
int count = 0;
for (int i = 0; i < size; i++) {
sum.arrayPtr[count] = arrayPtr[i];
sum.set[arrayPtr[i]] = true;
count++;
}
for (int j = 0; j < a.size; j++) {
if (!sum.isInSet(a.arrayPtr[j])) {
sum.arrayPtr[count] = a.arrayPtr[j];
sum.set[a.arrayPtr[j]] = true;
count++;
}
}
delete set;
delete arrayPtr;
arrayPtr = sum.arrayPtr;
set = sum.set;
size = sum.size;
return sum;

}
// ---------------------------------------------------------------------------
// operator*=
// overloaded *=: object = intersection of current object and parameter

intset& intset::operator*=(const intset& a) {

intset temp;
intset sec;
int count = 0;
for (int i = 0; i < size; i++) {
temp.arrayPtr[count] = arrayPtr[i];
temp.set[arrayPtr[i]] = true;
count++;
}
for (int j = 0; j < a.size; j++) {
if (temp.isInSet(a.arrayPtr[j])) {
sec.arrayPtr[count] = a.arrayPtr[j];
sec.set[a.arrayPtr[j]] = true;
count++;
}
}
delete set;
delete arrayPtr;
arrayPtr = sec.arrayPtr;
set = sec.set;
size = sec.size;
return sec;

}

// ---------------------------------------------------------------------------
// operator-=
// overloaded -=: object = difference of current object and parameter

intset& intset::operator-=(const intset& a) {

intset temp;
intset diff;
int count = 0;
for (int i = 0; i < size; i++) {
temp.arrayPtr[count] = arrayPtr[i];
temp.set[arrayPtr[i]] = true;
count++;
}
for (int j = 0; j < a.size; j++) {
if (!temp.isInSet(a.arrayPtr[j])) {
diff.arrayPtr[count] = a.arrayPtr[j];
diff.set[a.arrayPtr[j]] = true;
count++;
}
}
delete set;
delete arrayPtr;
arrayPtr = diff.arrayPtr;
set = diff.set;
size = diff.size;
return diff;

}


// ---------------------------------------------------------------------------
// operator=
// Overloaded assignment operator

intset& intset::operator=(const intset& right) {
if (&right != this) { // check for self-assignment
delete[] arrayPtr; // reclaim space
delete[] set;
copy(right);
}

return *this; // enables x = y = z;
}

// --------------------------------------------------------------------------
// Size the array, allocate memory, and copy contents of parameter's array

void intset::copy(const intset& toCopy) {
size = toCopy.size;
arrayPtr = new int[size];

for (int i = 0; i < size; i++) {
arrayPtr[i] = toCopy.arrayPtr[i];
}
}

// --------------------------------------------------------------------------
// operator==
// Determine if two arrays are equal.
bool intset::operator==(const intset& right) const {
if (size != right.size)
return false; // arrays of different sizes

for (int i = 0; i < size; i++)
if (arrayPtr[i] != right.arrayPtr[i])
return false; // arrays are not equal
return true; // arrays are equal
}

// --------------------------------------------------------------------------
// operator!=
// Determine if two arrays are not equal.
bool intset::operator!=(const intset& right) const {
return !(*this == right);
}


// ----------------------------------------------------------------------------
bool intset::isInSet(int element) {
if (element < 0) {
return false;
}
else if (set[element] == true) {
return true;
}
}
// --------------------------------------------------------------------------
// operator>>
// Overloaded input operator for class Array;
// inputs values for entire array.
istream& operator>>(istream& input, intset& a) {
for (int i = 0; i < a.size; i++) {
input >> a.arrayPtr[i];
if (a.arrayPtr[i] == -1) {
a.arrayPtr[i - 1] = NULL;
break;
}
}
return input; // e.g., enables cin >> x >> y;
}




// --------------------------------------------------------------------------
// operator<<
// Overloaded output operator for class Array
ostream& operator<<(ostream& output, const intset& a) {
int i;
for (i = 0; i < a.size; i++) {
if (a.arrayPtr[i] != NULL) {
output << a.arrayPtr[i] << ' ';
}
if ((i + 1) % 10 == 0) // display 10 per line
output << endl;
}

if (i % 10 != 0)
output << endl;
return output; // e.g., enables cout << x << y;
}
Hi welcome to the forum :+)

First up please always use code tags : www.cplusplus.com › articles › jEywvCM9

Three things I notice: Use of an ordinary array instead of a std::vector ; use of manual memory management and pointers; use of NULL. If you use a std::vector, it will implicitly allocate memory on the heap for you, and it is a full RAII container. Prefer to use nullptr rather than NULL

Here is an article about std::vector vs dynamic arrays:
http://www.cplusplus.com/articles/37Mf92yv/

In general try to avoid new / delete , malloc / free and pointers. If you are inventing a new type of container, then use new, but make sure they are consumed by a smart pointer. https://en.cppreference.com/w/cpp/memory/unique_ptr or a std::shared_ptr https://en.cppreference.com/w/cpp/memory/shared_ptr

In your case you can implement your set in terms of a std::vector, so no need for anything else.

in my main, the second one will cause error.


Always post your errors verbatim, so we don't have to guess. It is also helpful to post a minimal version of the code that can be compiled.

Have you tried using a debugger ? If not, it is a very useful skill to have.

Good Luck !! :+)
Topic archived. No new replies allowed.