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
};

Recommended Answers

All 2 Replies

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);

Each of your cpp files have an int main()!
There can only be 1 int main() in your entire program. Decide which of the cpp file have the int main() or introduce a main.cpp which only consists of int main().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.