0

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

2
  • Where is operator+? Commented Nov 5, 2017 at 1:56
  • If you want to use operator+() it needs to be declared. It isn't. Commented Nov 5, 2017 at 2:17

1 Answer 1

2

Here's the signature of your overloaded stream insertion operator:

friend std::ostream& operator<<(std::ostream &out, Set &set); 

Notice that you're taking your last parameter in as a non-const reference. This means that this function can only take in lvalues as its second parameter. This is fine for all the cases you've listed except this one:

cout << s1+s2 << endl; 

I don't believe you included the signature of your operator+ function in your code above, but I'd suspect that it (properly) returns a Set by value. This code gets translated into

(operator<< (cout, s1 + s2)) << endl; 

which triggers the issue, since the expression s1 + s2 doesn't evaluate to an lvalue.

To fix this, change the signature of your operator<< function t

friend std::ostream& operator<<(std::ostream &out, const Set &set); 

The added const here lets the last parameter bind to anything, including temporaries, which lets you print out s1 + s2 safely. Plus, it (correctly) indicates to the caller that the act of printing out a Set won't actually change that set.

As an aside, it's very weird to include a .cpp file at the top of another .cpp file. You should probably define a header for your Set type and include that. Otherwise, if multiple files try to include the Set.cpp file, you'll get linker errors due to multiple definitions of each of the functions.

Sign up to request clarification or add additional context in comments.

1 Comment

You assumed and didn't become an ass. Thank you! it fixed the issue and I updated my question to include the + operate override which I guess I didn't want to include.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.