I am having trouble removing the errors listed below please help

#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);
       
       rational Add(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::rational Add(rational r)
{
 rational l;
int result;
int a = l.getnum();
int b = l.getden();
int c = r.getnum();
int d = r.getden();
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;
}

(56): error C2146: syntax error : missing ';' before identifier 'Add'
(56): error C2761: '__ctor' : member function redeclaration not allowed

Recommended Answers

line 56: int rational::Add(rational r) What do you expect the Add() function to be doing? line 58 creates an uninitialized variable of type rational and you are using its random contents in lines 61 and 62. The result of that calculation on line 64 will be garbage.

Jump to Post

All 2 Replies

line 56: int rational::Add(rational r) What do you expect the Add() function to be doing? line 58 creates an uninitialized variable of type rational and you are using its random contents in lines 61 and 62. The result of that calculation on line 64 will be garbage.

Member Avatar

Some recommendations for enhancement:
1. operator + () instead of Add(),
2. operator << () instead of print()
3. operator >> () instead of input()

Be a part of the DaniWeb community

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