Need Help, getting an error and not sure how to fix it!

My assignment is
Develop rational number class that can
• add
• subtract
• multiply
• divide
• reduce to simplest form
Your class must be able to handle rational numbers of different denominators
Any help will be greatly appreciated!


This is the errors I am getting

error C4716: 'Rational::addition' : must return a value
error C4716: 'Rational::multiplication' : must return a value
error C4716: 'Rational::division' : must return a value
error C4716: 'Rational::subtraction' : must return a value


Last edited on
The error messages are clear enough. You have for example c.addition(d) in your test, but this function does not return anything, so z=c.addition(d); makes no sense. You need to return a rational number. You can put for example return *this; at the end of the functions. See the following example

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
#include<iostream>
using namespace std;
class R{
public:
   R(int a=0);
   R add(R b);
   int internal;
};
R::R(int a)
{
   internal=a;
}
R R::add(R b)
{
   internal+=b.internal;
   return *this;
}

int main()
{
   R a(1),b(2),c;
   cout<<a.internal<<endl;
   cout<<b.internal<<endl;
   cout<<c.internal<<endl;
   c=a.add(b);
   cout<<c.internal<<endl;
}


Also, your addition/subtraction is not what other people might expect. For example 1/2+1/3 in your functions will be 2/5 instead of 5/6
Topic archived. No new replies allowed.