My code gives the following errors when i try to run the program

I have to create a program that does various operations with rational numbers and prints the numbers as a/b and in decimal form.
Hi,
I am getting the following errors when I try to run my program and I have tired to fix them in every possible way I could think of but nothing seems to do the trick. I would be extremely grateful if you could help me because I have been trying to fix these erros for the past 4 hours (no lie). The errors I get are:
I get this error twice and the error refers toRational c(num1, den 1) and rational d (num2, den2) in my driver.cpp file:
In function `int main()':
no matching function for call to `Rational::Rational(int&, int&)'
candidates are: Rational::Rational(const Rational&)
Rational::Rational()
I also get this error:
[Build Error] [driver.o] Error 1

NOTE: The driver.cpp file was already provied to me so I CANNOT change it
DRIVER.CPP FILE:
//
// driver for rational.cpp
//
#include <iostream>
using namespace std;
#include "Rational.h"

int main()
{
// define numerators and denominators for a pair of rational numbers
int num1 = 7;
int den1 = 4;
int num2 = 3;
int den2 = 7;

// create rational numbers
Rational c( num1, den1 );
Rational d( num2, den2 );
// store result of calculation in object x
Rational x;

// print default object
cout << "By default, x is: ";
x.printRational();
cout << endl << endl;

c.printRational();
cout << " + ";
d.printRational();
x = c.addition( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit addition result: "
<< ( ( double(num1)/den1 ) + ( double(num2)/den2 ) )
<< endl << endl;

c.printRational();
cout << " - ";
d.printRational();
x = c.subtraction( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit subtraction result: "
<< ( ( double(num1)/den1 ) - ( double(num2)/den2 ) )
<< endl << endl;

c.printRational();
cout << " x ";
d.printRational();
x = c.multiplication( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit multiplication result: "
<< ( ( double(num1)/den1 ) * ( double(num2)/den2 ) )
<< endl << endl;

c.printRational();
cout << " / ";
d.printRational();
x = c.division( d );
cout << " = ";
x.printRational();
cout << '\n';
x.printRational();
cout << " = ";
x.printRationalAsFloating();
cout << "\nExplicit division result: "
<< ( ( double(num1)/den1 ) / ( double(num2)/den2 ) )
<< endl << endl;

getchar();
return 0;
}


RATIONAL.H FILE:
#ifndef RATIONAL_H
#define RATIONAL_H

#include <cstdlib>
#include <iostream>
using namespace std;

class Rational
{
public:
int num;
int den;
Rational();
Rational addition (const Rational&);
Rational subtraction (const Rational&);
Rational multiplication (const Rational&);
Rational division (const Rational&);
Rational printRational ();
Rational printRationalAsFloating();
private:

Rational reduction();
};
#endif // #ifndef DATE_H

RATIONAL.CPP FILE:
#include "Rational.h"
Rational::Rational(int numerator, int denominator) : num(numerator), den(denominator)
{
}
Rational::reduction(const Rational&a)
{
int smallest;
int gcd;
Rational t;
if (a.num <a.den)
smallest=a.num;
else
smallest=a.den;
for (int i=2; i<smallest;i++)
{
if (a.num%i==0&&a.den%i==0)
gcd=i;
if (gcd!=0)
{
t.num=a.num/gcd;
t.den=a.den/gcd;
}
}
}

Rational Rational::addition(const Rational&a)
{
Rational t;
t.num=a.num*den+a.den*num;
t.den=a.den*den;
t.reduction();
return t;
}
Rational Rational::subtraction (const Rational&a)
{
Rational t;
t.num=a.num*den-a.den*num;
t.den=a.den*den;
t.reduction();
return t;
}

Rational Rational::multiplication (const Rational&a)
{
Rational t;
t.num=a.num*den*a.den*num;
t.den=a.den*den;
t.reduction();
return t;
}

Rational Rational::division (const Rational&a)
{
Rational t;
t.num=a.num*den/a.den*num;
t.den=a.den*den;
t.reduction();
return t;
}
Rational Rational::printRational()
{
cout <<num<<"/"<<den ;
}
Rational Rational::printRationalAsFloating()
{
cout <<num/den



THANKS

[code] Your code goes here [/code]
If you not want to create the constructor Rational(int, int), just use the default
The attributes are publics, access them directly.

NOTE: The driver.cpp file was already provied to me so I CANNOT change it
eh? If you have the file you can change it.
Last edited on
I don't understand what you mean by just use the default and the driver.cpp file was given by my teacher to test my code for the other two files so my other two files have to match with driver.cpp in other words driver.cpp is used to test our program.
This is the only constructor declared in Rational.h Rational(); (that's the default constructor). However in Rational.cpp you have implemented only this Rational::Rational(int numerator, int denominator). ( They don't match )
Hi thanks alot. I was able to fix it and my program runs now. All I had to do was change rational in Rational.h to Rational::Rational(int numerator=0, int denominator=1);. It is able to do all the operations (+,-,*,/) properly after I changed my code around a bit. However, there is something wrong with my reduction function and my printRationalAsFloating function as it does not reduce the fraction or print it in decimal form. For the print as floating, I know the problem is that it prints the number as an int instead of a double. For example, it would print 7/4 as 1. Here is the new code that I have for rational.cpp:
#include "Rational.h"
Rational::Rational(int numerator, int denominator) : num(numerator), den(denominator)
{
}
void Rational::reduction()
{
int smallest;
int gcd;
Rational t;
if (num <den)
smallest=num;
else
smallest=den;
for (int i=2; i<smallest;i++)
{
if (num%i==0&&den%i==0)
gcd=i;
if (gcd!=0)
{
t.num=num/gcd;
t.den=den/gcd;
}
}
}

Rational Rational::addition(const Rational&a)
{
Rational t;
t.num=a.num*den+a.den*num;
t.den=a.den*den;
t.reduction();
return t;
}

Rational Rational::subtraction (const Rational&a)
{
Rational t;
t.num=a.den*num-a.num*den;
t.den=a.den*den;
t.reduction();
return t;
}

Rational Rational::multiplication (const Rational&a)
{
Rational t;
t.num=a.num*num;
t.den=a.den*den;
t.reduction();
return t;
}

Rational Rational::division (const Rational&a)
{
Rational t;
t.num=a.num*den;
t.den=a.den*num;
t.reduction();
return t;
}
Rational Rational::printRational()
{
cout <<num<<"/"<< den ;
}
Rational Rational::printRationalAsFloating()
{
cout << num/den;
}

Here is the output of my program after I run it:
By default, x is: 0/1

7/4 + 3/7 = 61/28
61/28 = 2
Explicit addition result: 2.17857

7/4 - 3/7 = 37/28
37/28 = 1
Explicit subtraction result: 1.32143

7/4 x 3/7 = 21/28
21/28 = 0
Explicit multiplication result: 0.75

7/4 / 3/7 = 12/49
12/49 = 0
Explicit division result: 4.08333
Again [code] Your code goes here [/code]

1
2
3
4
5
Rational Rational::printRationalAsFloating() //you are telling that you return a Rational
{
cout << num/den; //that's integer division try num/(float) den
//but you are not returning anything 
} 


About reduction, change your gcd function http://en.wikipedia.org/wiki/Euclidean_algorithm
Thank you. You have been so helpful. I will relook at my gcd function and the floating function did work except I used a double instead of float which produces the same output.
Hi I looked at the wikipedia link you sent me but I still cannot get the correct output for my gcd. Can you help me out a bit more.
Topic archived. No new replies allowed.