Im working on a class with fractions. I have a method called plus that takes another fraction as an argument and returns the sum of the receiver and the argument. For example, if r = 2/3 and s = 3/4, then r.plus(s) returns another fraction object whose value is 17/12. Note that both the receiver and argument are left unchanged. The returned fraction is not reduced.
I have
int plus( const Fraction & f2 ) const { return ( ( b * f2.a ) + ( a * f2.b ) ) << '/' << ( b * f2.b ); }
I am getting the warning in the title.
I think my problem is the << '/' << in the return part.
But without that im not sure how to return a fraction that would look like 17/12
Well you are returning an int, which doesn't happen to include '/'s
What you may wish to do is change your return type to a string and compose the string (using osstringstream) when you return it.
or you could make a new class fraction with a numerator and denominator
edit: oh you may want to revise your title, if you can.
Notice the const instance return of the operator. This is to guard against silliness like:
1 2
Fraction a, b, c;
(a + b) = c;
Props on the const correctness, though! I rarely see people new to C++ use const and & in their classes. I didn't use them much either when I was new. But, when I got to really writing a LOT of code, they started to naturally make sense.