Rational numbers

Jun 24, 2009 at 10:52pm
Can someone please help!! I have two errors and one warning (listed below)and I am having a hard time correcting them. Any advice will be appreciated!!

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

class rational
{
public:
rational(int num, int den);
rational(int num);
rational();

int getnum();
int getden();
void input(istream& in);
void print(ostream& out);
bool less(rational r);

rationalAdd(rational r);

private:
int numerator;
int denomerator;

};

int rational::getnum()
{
return numerator;
}


int rational::getden()
{
return denomerator;
}

rational::rational(int num, int den)
{
numerator = num;
denomerator = den;
}

rational::rational(int num)
{
numerator = num;
denomerator = 1;
}

rational::rational()
{
numerator = 1;
denomerator = 1;
}

int rational::rationalAdd(rational r)
{
rational l;
int a = l.getnum();
int b = l.getden();
int c = r.getnum();
int d = r.getden();
rational result = ((a * d + b * c) / (b * d));
return result;
}

void rational::input(istream& in)
{
int numerator;
int denomerator;
char slash;
cin >> numerator >> slash >> denomerator;
return;
}


void rational::print(ostream& out)
{
cout<<numerator<<"/"<<denomerator;
}


int main()
{
int a, b, c, d;
cout << "Enter a numerator: " << endl;
cin >> a;
cout << "Enter a denominator: " << endl;
cin >> b;
cout << "Enter another numerator: " << endl;
cin >> c;
cout << "Enter another denominator: " << endl;
cin >> d;
rational num1(a, b);
rational num2(c, d);
num1.Add(num2);
num1.print(cout);
system("pause");
return 0;
}

(96): error C2039: 'Add' : is not a member of 'rational'
(64): error C2440: 'return' : cannot convert from 'rational' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
(19): warning C4183: 'rationalAdd': missing return type; assumed to be a member function returning 'int'

Jun 24, 2009 at 11:04pm
Everything explained clearly in error messages.

96): error C2039: 'Add' : is not a member of 'rational'

There's no function named 'Add' in class 'rational' . You have rationalAdd function instead.

and please use code tags.

Jun 25, 2009 at 4:24am
Also, your function int rational::rationalAdd(rational r) doesn't return the sum of the two rational numbers but instead a number with denominator 1, proportional to the desired number (but not equal).
Last edited on Jun 25, 2009 at 4:24am
Jun 25, 2009 at 12:11pm
Unfortunately not every programmer knows one can format forum (esp. C++ forum) posts to make codes more easy to read...and more easy to find which line is which...

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

class rational
{
public:
rational(int num, int den);
rational(int num);
rational();

int getnum();
int getden();
void input(istream& in);
void print(ostream& out);
bool less(rational r);

rationalAdd(rational r);

private:
int numerator;
int denomerator;

};

int rational::getnum()
{
return numerator;
}


int rational::getden()
{
return denomerator;
}

rational::rational(int num, int den) 
{
numerator = num;
denomerator = den;
}

rational::rational(int num)
{
numerator = num;
denomerator = 1;
}

rational::rational()
{
numerator = 1;
denomerator = 1;
} 

int rational::rationalAdd(rational r)
{
rational l;
int a = l.getnum();
int b = l.getden();
int c = r.getnum();
int d = r.getden();
rational result = ((a * d + b * c) / (b * d));
return result;
}

void rational::input(istream& in)
{
int numerator;
int denomerator;
char slash;
cin >> numerator >> slash >> denomerator;
return;
}


void rational::print(ostream& out)
{
cout<<numerator<<"/"<<denomerator;
}


int main() 
{
int a, b, c, d;
cout << "Enter a numerator: " << endl;
cin >> a;
cout << "Enter a denominator: " << endl;
cin >> b;
cout << "Enter another numerator: " << endl;
cin >> c;
cout << "Enter another denominator: " << endl;
cin >> d;
rational num1(a, b);
rational num2(c, d);
num1.Add(num2);
num1.print(cout);
system("pause");
return 0;
}


and indentation...


On line 64, you are returning a rational

and as tition mentioned, the RHS of = on line 63 gives you an int, this is not intended.
Instead figure out how you get both the num and the den of the result, then return the result by value.
Topic archived. No new replies allowed.