Nov 2, 2012 at 3:54am UTC
hey there, this is my code and I need help figuring why im getting an error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
//fraction.h
#include <iostream>
#include <iomanip>
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 Nov 2, 2012 at 3:55am UTC
Nov 2, 2012 at 4:10am UTC
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.
Nov 2, 2012 at 4:29am UTC
makes a little sense; wonder how i can fix it! Google here i come! :P
Nov 2, 2012 at 5:03am UTC
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.
Nov 2, 2012 at 5:17am UTC
Yeah you're correct, my bad. I'm thinking of operator += which does modify this. Thanks for the extra info.
Last edited on Nov 2, 2012 at 5:17am UTC