I seemed to be having an overloading operator issue. I can't really make out what the error is trying to say. Here's the error:
Error: no match for 'operator<<' in 'std::cout << s1.Set::operator+(((const Set&)((const Set*)(& s2))))' Here's my code:
#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); } I overload both the + and >> operators. When executing the code, would it not execute the overloaded + operator first then the >> operator.
Just need some clarification. Thanks
operator+?operator+()it needs to be declared. It isn't.