I get two erros, 'const class Fraction' has no member named 'n' & 'const class Fraction' has no member named 'd'
Well, look at the offending line in Fract.h
:
Fraction(const Fraction &src) { set(src.n, src.d); }
Where are n
and d
defined in Fraction
? Nowhere; this is why the error. The other member functions that use n
and d
get them as arguments, but this construction doesn't have them. I'm sure you want:
Fraction(const Fraction &src) { set(src.num, src.den); }
When I compile it on the command line with g++ -Wall FloatFraction2.cpp Fract7.cpp -o FloatFraction I get more errors than I care to look at.
Start caring; compiler errors and warnings are there for a reason. Read the error message, look at the line it's complaining about, and see if you can figure out why it's a problem. If you're having trouble, post the specific error and we'll try to help.
I copies this code straight from my book (C++ Without Fear 2nd Edition) so I don't understand why it wouldn't work.
By hand? The errors I see look like simple transcription problems:
At Fract7.cpp:111
:
return (num == other.num && den == other, den);
Looks like you typed ,
where you meant .
; it should be:
return (num == other.num && den == other.den);
Same thing at Fract7.cpp:106
:
return Fraction(num * other, num, den * other.den);