When I try to compile the program below in Eclipse I get two erros, ‘const class Fraction’ has no member named ‘n’ & ‘const class Fraction’ has no member named ‘d’
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. I copies this code straight from my book (C++ Without Fear 2nd Edition) so I don't understand why it wouldn't work.
FloatFraction2.cpp
#include <iostream>
#include "Fract.h"
using namespace std;
class FloatFraction : public Fraction
{
public:
double float_val;
// Inherifted constructors: with C++ 0X,
// these can all be replaced with using Fraction::Fraction
FloatFraction() { set(0, 1); }
FloatFraction(int n, int d) { set(n, d); }
FloatFraction(int n) { set(n, 1); }
FloatFraction(const FloatFraction &src) { set(src.get_num(), src.get_den()); }
FloatFraction(const Fraction &src) { set(src.get_num(), src.get_den()); }
void normalize(); // overridden
};
void FloatFraction::normalize()
{
Fraction::normalize();
float_val = (double) get_num() / get_den();
}
int main()
{
FloatFraction fract1(1, 4), fract2(1, 2);
FloatFraction fract3 = fract1 + fract2;
cout << "1/4 + 1/2 = " << fract3 << endl;
cout << "Floating pt value is = ";
cout << fract3.float_val << endl;
}
Fract7.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
class Fraction
{
private:
int num, den;
public:
Fraction() { set(0, 1); }
Fraction(int n, int d) { set(n, d); }
Fraction(int n) { set(n, 1); }
Fraction(const Fraction &src);
void set(int n, int d) { num = n; den = d; normalize(); }
int get_num() const { return num; }
int get_den() const { return den; }
Fraction add(const Fraction &other);
Fraction mult(const Fraction &other);
Fraction operator+(const Fraction &other) { return add(other); }
bool operator==(const Fraction &other);
friend ostream &operator<<(ostream &os, Fraction &fr);
private:
void normalize(); // convert to standard form
int gcf(int a, int b); // greatest common factor
int lcm(int a, int b); // lowest common denom
};
int main()
{
Fraction f1(1, 2);
Fraction f2(1, 3);
Fraction f3 = f1 + f2 + 1;
cout << "1/2 + 1/3 + 1 = " << f3 << endl;
return 0;
}
Fraction::Fraction(Fraction const &src)
{
num = src.num;
den = src.den;
}
// normalize: put fraction into standard from, unique for
// each mathematically different value.
void Fraction::normalize()
{
// handle cases involving 0
if(den == 0 || num == 0)
{
num = 0;
den = 1;
}
// put neg. sign in numerator only.
if(den < 0)
{
num *= -1;
den *= -1;
}
// factor out GCF from numerator and denominator
int n = gcf(num, den);
num = num / n;
den = den / n;
}
// Greatest COmmon Factor
int Fraction::gcf(int a, int b)
{
if(b == 0)
return abs(a);
else
return gcf(b, a%b);
}
// Lowest Common Multiple
int Fraction::lcm(int a, int b)
{
int n = gcf(a, b);
return a / n * b;
}
Fraction Fraction::add(const Fraction &other)
{
int lcd = lcm(den, other.den);
int quot1 = lcd / den;
int quot2 = lcd / other.den;
return Fraction(num * quot1 + other.num * quot2, lcd);
}
Fraction Fraction::mult(const Fraction &other)
{
return Fraction(num * other,num, den * other.den);
}
bool Fraction::operator==(const Fraction &other)
{
return (num == other.num && den == other,den);
}
// Fraction Class Friend Function
ostream &operator<<(ostream &os, Fraction &fr)
{
os << fr.num << "/" << fr.den;
return os;
}
Fract.h
#include <iostream>
using namespace std;
class Fraction
{
private:
int num, den; // numerator and denominator
public:
Fraction() { set(0, 1); }
Fraction(int n, int d) { set(n, d); }
Fraction(int n) { set(n, 1); }
Fraction(const Fraction &src) { set(src.n, src.d); }
void set(int n, int d) { num = n; den = d; normalize(); }
int get_num() const { return num; }
int get_den() const { return den; }
Fraction add(const Fraction &other);
Fraction mult(const Fraction &other);
Fraction operator+(const Fraction &other) { return add(other); }
Fraction operator*(const Fraction &other) { return mult(other); }
int operator==(const Fraction &other);
friend ostream &operator<<(ostream &os, Fraction &fr);
protected:
virtual void normalize(); // put in standard form
private:
int gcf(int a, int b); // greatest common factor
int lcm(int a, int b); // lowest common denom
};