オーバーロードのオペレータの問題が発生しているようです。私は、エラーが何を言おうとしているのか本当に分からない。ここではエラーがあります:オーバーロードの問題C++
Error: no match for 'operator<<' in
'std::cout << s1.Set::operator+(((const Set&)((const Set*)(& s2))))'
ここに私のコードは次のとおりです。
#include "Set.cpp"
int main(int argc, char *argv[]){
Set s1(10),s2(6),s3(3),s4;
cout<<"First set ({x,y,z}): ";
cin>>s1;
cout<<"A: "<<s1<<endl;
cout<<"Second set: ";
cin>>s2;
cout<<"B: "<<s2<<endl;
cout<<s1+s2<<endl;
}
class Set {
private:
bool elements[255];
int capacity; //Yes, an unsigned char, short, or even size_t, would be better
Set(const bool elements[255], int capacity); //Helpful for immutable types
public:
Set();
Set(short capacity);
friend std::ostream& operator<<(std::ostream &out, Set &set);
friend std::istream& operator>>(std::istream &in, Set &set);
int getCapacity() const; //Cardinality of universe. i.e. |Universe| (or just 'capacity')
};
Set::Set(const bool elements[255], int capacity){
this->capacity = capacity;
for(int i=0; i<255;i++){
if(elements[i] == true && i <= capacity){
this->elements[i] = true;
}
else{
this->elements[i] = false;
}
}
}
Set::Set(short capacity){
this->capacity = capacity;
}
std::ostream& operator<<(std::ostream &out, Set &set) {
int capacity = set.getCapacity();
out<<"{";
for(int i=0; i < 255; i++){
if(set.elements[i] == true){
out<<i<<",";
}
}
out<<"}";
return out;
}
std::istream& operator>>(std::istream &in, Set &set) {
bool arr[255];
int cap=set.getCapacity();
char open;
in>>open;
if (in.fail() || open!='{') {
in.setstate(std::ios::failbit);
return in;
}
for (int i=0;i<cap;i++)
arr[i]=false;
std::string buff;
std::getline(in,buff,'}');
std::stringstream ss(buff);
std::string field;
while (true) {
std::getline(ss,field,',');
if (ss.fail()) break;
int el;
std::stringstream se(field);
se>>el;
if (el>=0&&el<cap){
arr[el]=true;
}
else{
arr[el]=false;
}
}
set=Set(arr,cap);
}
Set Set::operator+(const Set &other) const{
bool arr[255];
for(int i=0; i<255;i++){
if(this->elements[i] == true || other.elements[i]==true)
arr[i] == true;
}
int capacity = this->capacity>=other.capacity?this->capacity:other.capacity;
return Set(arr,capacity);
}
私は+と>>演算子の両方をオーバーロードします。コードを実行するとき、まずオーバーロードされた+演算子を実行してから、>>演算子を実行しません。
説明が必要です。あなたは非const参照としてのあなたの最後のパラメータを取っている
friend std::ostream& operator<<(std::ostream &out, Set &set);
お知らせ:ありがとう
'operator +'はどこですか? – Pixelchemist
'operator +()'を使いたい場合は、それを宣言する必要があります。そうではありません。 – Peter