0

I try to realize difference of sets with "-". For example:

Set a = 1 2 3 4;
Set b = 3 4 5 6;
Set c = a - b; // (1 2);

How i should overload operator? I tried to use frienldy function, but it can't work with variable "size". Visual Studio show error "c2597" at 28th line (size++;) I can't understand, how to overloading "-" for using it with two sets. When I overload method of class, i can use only one argument. When i use friend-function, i can use twi arguments (Set a, Set b), but i can't work with variable "size".

#include <iostream>
#include <locale.h>
#include <conio.h>
#include <vector>
using namespace std;

class Set {
private:
    int size;
    vector <int> vect;  
public:
    Set() { size = 0; } 
    ~Set() { vect.clear(); } 
    void Enter(); 
    void Show(); 
    friend Set operator-(Set a, Set b)
    {
        size = 0;
        vect.clear();
        int i, j, n = 0;
        for (i = 0; i < a.size; i++) {
            int cnt = 0;
            for (j = 0; j < b.size; j++)
            {
                if (a.vect[i] == b.vect[j]) cnt++;
            }
            if (cnt == 0) {
                size++;
                vect.push_back(a.vect[i]);
            }

        }
        return a;
    }
    void add()
    {
        int element;
        cout << "Введите новый элемент " << endl;
        cin >> element;
        size = size + 1;
        vect.push_back(element);
    }
};

void Set::Enter() {
    cout << "Введите размер " << endl;
    cin >> size;
    vect.resize(size);
    cout << "Введите элементы :" << endl;
    for (int i = 0; i < size; i++)
    {
        cin >> vect[i];
    }
}

void Set::Show() {
    cout << "Множество: " << endl;
    for (int i = 0; i < size; i++)
        cout << vect[i] << " ";
    cout << endl;
}


int main() {
    setlocale(LC_ALL, "RUS");
    Set a;
    a.Enter();
    Set b;
    b.Enter();
    Set c;
    c = a - b;
    c.Show();
    c.add();
    c.Show();
    _getch();
    return 0;
}

UPD: I made it through method:

Set operator-(const Set& b)
    {
        Set a = *this;
        Set tmp;
        tmp.size = 0;
        vect.clear();
        int i, j, n = 0;
        for (i = 0; i < a.size; i++) {
            int cnt = 0;
            for (j = 0; j < b.size; j++)
            {
                if (a.vect[i] == b.vect[j]) cnt++;
            }
            if (cnt == 0) {
                tmp.size++;
                tmp.vect.push_back(a.vect[i]);
            }

        }
        return tmp;
    }
coi175
  • 29
  • 4
  • So what you want is a [set difference](https://en.cppreference.com/w/cpp/algorithm/set_difference)? – Some programmer dude Feb 21 '20 at 13:18
  • FYI: [SO: how to find the intersection of two std::set in C++?](https://stackoverflow.com/a/45079226/7478597) where I overloaded `*` for intersection of sets and `+` for union of sets (just for fun). – Scheff's Cat Feb 21 '20 at 13:20
  • You also have some other problems, like the operator function not being a member function which means it doesn't have the `vect` or `size` variables defined. And you always returning `a` without modifying it. – Some programmer dude Feb 21 '20 at 13:20
  • Notice that `std::vector` already has `size()`. – Jarod42 Feb 21 '20 at 13:23
  • I tried to return a, because didn't understand, which method must return)) Sorry for my English) difference: {1,2,3} - {2,3,4} = {1} – coi175 Feb 21 '20 at 13:54

1 Answers1

0

friend functions are not member methods, so you have to remove/replace usage of (implicit) this as size = 0, you might add extra object:

friend Set operator-(Set a, Set b)

{
    Set res;

    for (int i = 0; i < a.size; i++) {
        int cnt = 0;
        for (int j = 0; j < b.size; j++)
        {
            if (a.vect[i] == b.vect[j]) cnt++;
        }
        if (cnt == 0) {
            res.size++;
            res.vect.push_back(a.vect[i]);
        }

    }
    return res;
}
Jarod42
  • 173,454
  • 13
  • 146
  • 250