C++のクラスについての質問です。
①friend bool operator <=, >=, ==のところなのですが.
①(<=).もしs1の中にs2の数字が全てあるならReturn True.
②(>=).もしs2のなかにs1の数字が全てあるなら Return true.
③(==)もしs1とs2の中の数字が一緒なら Return true にしたいのですがうまくいきません。
②void remove アレイから特定の数字を抜く(この場合はs4の中から99を抜く)と
void clear アレイの数字を全て消す(s1の中の全ての数字)
というファンクションを作りたいのですが全くうまくいきません。(そもそもvoidで正しいのでしょうか…?)
エラーが出るのは②だけです。分かる方がいたら教えてもらえると助かります。よろしくお願いします。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
class MySet
{
public:
MySet();
friend bool operator ^ (int element, const MySet &s);
friend MySet operator + (MySet s, int element);
friend MySet operator + (const MySet &s1, const MySet &s2);
friend ostream& operator << (ostream &out, const MySet &s);
friend MySet operator * (const MySet &s1, const MySet &s2);
friend MySet operator - (const MySet &s1, const MySet &s2);
friend bool operator <= (const MySet &s1, const MySet &s2); // subset
friend bool operator >= (const MySet &s1, const MySet &s2); //supterset
friend bool operator == (const MySet &s1, const MySet &s4);
void remove(int element, const MySet &s);
void clear(const MySet &s);
int size() const;
private:
int numbers[100];
int count; //have to be size?
};
int main()
{
MySet s1;
cout << "s1: " << s1 << endl; //should print {}
if (s1.size() == 0) {
cout << "Set 1 is empty" << endl;
}
s1 = s1 + 10; //add an element to s1
s1 = s1 + 10;
s1 = s1 + 20;
cout << "S1: " << s1 << endl;//should print {10, 20}
if (10 ^ s1) //test membership
{
cout << "10 is an element of S1: " << s1 << endl;
}
MySet s2;
//add elements to s2
s2 = s2 + 10;
s2 = s2 + 40;
s2 = s2 + 50;
s2 = s2 + 20;
cout << "S2: " << s2 << endl;
//S3 is the union of s1 and s2
MySet s3 = s1 + s2;
cout << "S3 (S1 + S2): " << s3 << endl;
//s3 is the intersection of s1 and s2
s3 = s1 * s2;
cout << "S3 (S1 * S2): " << s3 << endl;
//s3 is the differenct of s1 and s2
s3 = s1 - s2;
cout << "S3 (S1 - S2): " << s3 << endl;
//test the subset operation
if (s1 <= s2)
{
cout << "S1 is a subset of S2" << endl;
}
//test the superset operation
if (s2 >= s1)
{
cout << "S2 is a superset of s1" << endl;
}
MySet s4;
s4 = s4 + 20;
s4 = s4 + 10;
cout << "S4: " << s4 << endl;
//test equality
if (s1 == s4)
{
cout << "S1 equals S4" << endl;
}
s4 = s4 + 99;
s4 = s4 + 100;
cout << "S4: " << s4 << endl;
s4.remove(99);
cout << "S4 after removing 99: " << s4 << endl;
s1.clear();
cout << "After clearing s1: " << s1 << endl; //should print {}
return 0; //or EXIT_SUCCESS
}
MySet::MySet()
{
count = 0 ;
}
bool operator ^ (int element, const MySet &s)
{
for(int i = 0; i < s.count; ++i)
{
if(element == s.numbers[i])
{
return true;
}
}
return false;
}
MySet operator + (MySet s, int element)
{
if(!(element ^ s)) //if one is T and other is F = T, both T or F = F
{
s.numbers[s.count] = element;
s.count++;
}
return s;
}
MySet operator + (const MySet &s1, const MySet &s2)
{
MySet s3;
for(int i = 0; i < s1.count; ++i)
{
s3 = s3 + s1.numbers[i];
}
for(int i = 0; i < s2.count; ++i)
{
s3 = s3 + s2.numbers[i];
}
return s3;
}
ostream& operator << (ostream &out, const MySet &s)
{
out << "{";
for(int i = 0; i < s.count; ++i)
{
out << s.numbers[i];
if(!(i == (s.count - 1)))
{
out << ", ";
}
}
out << "}";
return out;
}
MySet operator * (const MySet &s1, const MySet &s2)
{
MySet s3;
for(int i = 0; i < s1.count; ++i)
{
if(s1.numbers[i] ^ s2)
{
s3 = s3 + s1.numbers[i];
}
}
return s3;
}
MySet operator - (const MySet &s1, const MySet &s2)
{
MySet s3;
for(int i = 0; i < s1.count; ++i)
{
if(!(s1.numbers[i] ^ s2))
{
s3 = s3 + s1.numbers[i];
}
}
return s3;
}
bool operator <= (const MySet &s1, const MySet &s2) // subset
{
for(int i = 0; i < s1.count; ++i)
{
if(!(s1.numbers[i] ^ s2))
{
return false;
}
}
return true;
}
bool operator >= (const MySet &s1, const MySet &s2) //supterset
{
for(int i = 0; i < s2.count; ++i)
{
if(!(s2.numbers[i] ^ s1))
{
return false;
}
}
return true;
}
bool operator == (const MySet &s1, const MySet &s4)
{
for(int i = 0; i < s1.count; ++i)
{
if(!(s1.numbers[i] ^ s4))
{
return false;
}
}
for(int i = 0; i < s4.count; ++i)
{
if(!(s4.numbers[i] ^ s1))
{
return false;
}
}
return true;
}
int MySet::size() const
{
return count;
}
void MySet::remove(int element, const MySet &s)
{
for(int i = 0; i < s.count; ++i)
{
if(s.numbers[i] == element)
{
s.numbers[i] = NULL;
}
}
}
void MySet::clear(const MySet &s)
{
s.clear();
}