fraction class help

line 66
error: call of overloaded 'abs(int&)' is ambiguous


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

class Fraction{
    private:
        int num;    // numerator
        int den;    // denominator

    public:
    void set(int n, int d)
        {num = n; den = d; normalize();}
    int getNum()
        {return num;}
    int getDen()
        {return den;}

    private:
        void normalize();       // converts to standard form
        int gcf(int a, int b);  // greatest common factor
        int lcm(int a, int b);  // lowest common denominator
};

int main(){
    int a, b;
    string str;
    Fraction fract;
    while (true){
        cout << "Enter numerator: ";
        cin >> a;
        cout << "Enter denominator: ";
        cin >> b;
        fract.set(a, b);
        cout << "Numerator is   " << fract.getNum() << endl;
        cout << "Denominator is " << fract.getDen() << endl;
        cout << "Do again? (Y or N): ";
        cin >> str;
        if (!(str[0] == 'Y' || str[0] == 'y'))
            break;
    }
    return 0;
}
/* normalize: put fraction into standard form, unique for each mathematically different value
*/
void  Fraction::normalize(){
    // handle cases involving 0
    if (den == 0 || num == 0){
        num = 0;
        den = 1;
    }
    // put negative sign in numerator only
    if (den < 0){
        num *= (-1);
        den *= (-1);
    }
    // factor out GCF from numerator and denominator
    int n = gcf(num, den);
    num /= n;
    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;
}
Last edited on
Whats the % do?
How do you use the *, and can you explain how to use them for something other than *x=&y which as far as I know is the same as x=y.
Pointers seem to be far beyond my natural mental comprehension limitations.
% modulo
divides two numbers and returns the remainder

nd i have no clue what you're asking in your second post.

btw (to the person who will help me) the program runs if i take out the absolute value function. [and i know the program does nothing useful.. for now]
The 'int' overload of abs is in cstdlib. Only the floating point overloads are in cmath. Try adding
#include <cstdlib>
Last edited on
thanks Disch ^_^
Topic archived. No new replies allowed.