hey there, this is my code and I need help figuring why im getting an error:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` `````` //fraction.h #include #include using namespace std; class fraction { private: int numerator; int denominator; int f1; int f2; int f3; int f4; int f5; public: fraction() : numerator(), denominator(){} //void print(); fraction add(fraction f2); //void read(); friend ostream& operator<< (ostream& out, fraction& f) { out << f.numerator << "/" << f.denominator; return out; } friend istream& operator >>(istream& in, fraction& f); fraction (int n) : numerator (n), denominator (1) {}; friend ostream& add(fraction& f1, fraction& f2) { fraction f3 = f1 + f2; cout << f3 << "=" << f1 << "+" << f2 << endl; fraction f4 = f2 + 2; cout << f4 << "=" << f2 << "+" << 2 << endl; fraction f5 = 2 + f1; cout << f5 << "=" << 2 << "+" << f1 << endl; } };``````

the errors are on 36, 38 and 40. and it says
"no operator "+" matches these operands"
any help is appreciated.
Last edited on
On those lines you are attempting to use the + operator to add a fraction object to another fraction object but you haven't defined what that + operator would do.
makes a little sense; wonder how i can fix it! Google here i come! :P
If you need help finding it, look for overloading operators. The definition will look something like this I believe:
 ``12345`` ``````fraction& operator+(const fraction& rhs) //short for right hand side. { //add them together return *this; }``````
Almost. The operator+() should be a const method (it should not modify this; otherwise something like 2+3 would be illegal), and as a result you should create a temporary to hold the sum and return that.
Yeah you're correct, my bad. I'm thinking of operator += which does modify this. Thanks for the extra info.
Last edited on
Topic archived. No new replies allowed.