trying to create a fraction

error no matching function for call to 'Fraction::Output()'
not sure if im using constructors the correct way or default constructors the correct way as far as trying to call them in the main

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
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class Fraction
{
    public:
    Fraction()
    {
        den = 1, num = 1;
    }
    Fraction(double Num, double Den)
    {
        Num = num;
        Den = den;
        if (Den == 0)
        {
            cout << "Zero cannot be in the Denominator";
        }
    }
    double getNums()
    {
        return den;
        return num;
    }
    double Reciprical (double num, double den)
    {
        double rev_frac1 = num;
        double rev_frac2 = den;
        return rev_frac2 == rev_frac1;

    }
    void Output(double num, double den)
    {
        cout << num << "/" << den;
    }

private:
    double den;
    double num;
};


 int main()
 {
     Fraction frac(1,2);
     double frac1 = frac.Output();
     cout << "frac1 = " << frac1 << "\n";

     double numerator, denominator;
     cout << "Please enter a numerator!";
     cin >> numerator;
     cout << "Please enter a denominator!";
     cin >> denominator;

     Fraction frac(numerator, denominator);
     double user_f = frac.Output();
     cout << "Your Fraction = " << user_f << "\n"

     Fraction frac(2,3);
     double rec_f = frac.Output();
     cout << "Reciprical Frac = " << rec_f << "\n";

 }

Last edited on
Your declaration for Output says it takes two doubles.
 
void Output(double num, double den)


When you call it, you don't pass any arguments.
 
double user_f = frac.Output();

The compiler does not know about any function named Output that takes no arguments.

Why are you declaring Output with two arguments? Since Output is a member of your class, Output should operate on the member variables of the class, not arguments to the function.

BTW, what happens if the user enters 0 for the denominator? Your constructor does check for a denominator of zero. If zero, it puts out a message, but the code continues on its merry way. You will get a divide by zero trap if you ever decide to convert the fraction to a number.

Your assignment statements are reversed.
1
2
3
4
Fraction(double Num, double Den)
{
Num = num;    // num should be on the left
Den = den;      // den should be on the left 


Only the first return statement is going to be executed here. You will never reach the second return statement. Your compiler should have warned you about this.
1
2
3
4
double getNums()
{  return den;
    return num;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.