[] and * operator overloading for a Rational class in c++ - overloading

I'm having difficulty trying to overload the [] and * operators. This is the given code:
#include<iostream>
class Rational {
public:
Rational (int numerator=0, int denominator = 1) : n(numerator), d(denominator) { }
private:
int n, d;
};
std::ostream& operator<<(std :: ostream& out, const Rational& r) {
return out << r[0] << '/' << r[1] << std::endl;
}
int main() {
Rational rat1(2,3), rat2(3,4);
std::cout << rat1 << std::endl;
std::cout << rat1 * rat2 << std::endl;
}
Please overload [] and * operator for class Rational
I tried the following:
#include<iostream>
class Rational {
public:
Rational (int numerator=0 , int denominator = 1) :
n(numerator),d(denominator)
{ }
int operator[](int i){
if(i==0)
return n;
else return d;
}
Rational& operator*(Rational& rhs){
return *this(n*(rhs.n),d*(rhs.d));
}
private:
int n,d;
};
std::ostream& operator<<(std :: ostream& out , const Rational& r) {
return out<<r[0]<<'/'<<r[1]<<std::endl;
}
int main(){
Rational rat1(2,3) , rat2(3,4);
std::cout<<rat1<<std::endl;
std::cout<<rat1*rat2<<std::endl;
}
After trying this I get the following errors:
error: passing ‘const Rational’ as ‘this’ argument of ‘int Rational::operator[](int)’ discards qualifiers [-fpermissive]
return out<<r[0]<<'/'<<r[1]<<std::endl;
error: passing ‘const Rational’ as ‘this’ argument of ‘int Rational::operator[](int)’ discards qualifiers [-fpermissive]
return out<<r[0]<<'/'<<r[1]<<std::endl;
What am I doing wrong?

The problem is this line:
return *this(n * (rhs.n), d * (rhs.d));
It should be
return * new Rational(n * rhs.n, d * rhs.d);
You need to create and return a new Rational by calling the constructor, which is never done via the this pointer.
You should also validate the input: rat1[6] should generate an error.

Related

addition and subtraction of two numbers using operator overloading

#include<iostream>
using namespace std;
class add
{
private: int a,b;
public: add(int x=0)
{
a=x;
}
add operator+(add const &c) // sub operator-(sub const &c)
{ //{
add sum; // sub diff;
sum.a=a+c.a; // diff.a=a-c.a;
return sum; // return diff
} //}
void print()
{
cout<<"sum: "<<a;
}
};
int main()
{
add a1(10),a2(5); //sub s1(10),s2(5);
add a3=a1+a2; // sub s3=s1-s2;
a3.print(); // s3.print();
return 0;
}
Here I've written seperately but what to do if I need to do both in a single code?
I want a C++ code to perform them simultaneously
You can define any reasonable combination of:
Foo operator+(arg);
Foo operator-(arg);
Foo operator*(arg);
Foo operator/(arg);
And the arg can be another Foo or some other type entirely. For instance:
#include <iostream>
using namespace std;
class Operators {
public:
Operators() = default;
Operators(int v) : value(v) {}
Operators operator+(const Operators &other) {
return Operators{value + other.value};
}
Operators operator+(const int byValue) {
return Operators{value + byValue};
}
Operators operator-(const Operators &other) {
return Operators{value - other.value};
}
Operators operator-(const int byValue) {
return Operators{value - byValue};
}
Operators operator*(const Operators &other) {
return Operators{value * other.value};
}
Operators operator/(const Operators &other) {
return Operators{value / other.value};
}
int value = 0;
};
int main(int, char **) {
Operators first{10};
Operators second{20};
Operators result1 = first + second;
Operators result2 = first * second;
Operators result3 = first * 3;
Operators result4 = second / 2;
cout << "first + second == " << result1.value << endl;
cout << "first * second == " << result2.value << endl;
cout << "first * 3 == " << result3.value << endl;
cout << "first / 2 == " << result4.value << endl;
}
first + second == 30
first * second == 200
first * 3 == 30
first / 2 == 10
You'll see I overwrote operators that take two Operators objects, but I also wrote a few that take an integer argument, too.
I compiled and ran that with:
g++ --std=c++17 Whatever.cpp -o Whatever && Whatever

Writing a fraction class and I cannot figure out these errors

I'm writing a fraction.cpp and fraction.h that inputs fraction types and does mathematical operations on them. I have a weird error that says "undefined reference", but the reference is defined as far as I can see. There's also a bunch of errors that say "no matching function for call to fraction::outputFormat(fracForm)" and "candidate is" and others. Here is all of my code for reference:
main.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "fraction.h"
int main()
{
fraction f0;
fraction f1;
cin >> f0; // input format numerator/denominator, ex 23/9
cout << "input: " << f0 << endl; // outputs what was input
// Note the << and >> operators should also work for file streams
// cin >> f0 >> f1; // reads 2 fractions, ex 8/5 12/16
f1.setFraction(17,6); // 17/6
cout << f1.getNumerator() << '/' << f1.getDenominator() << endl; // 17/6
f1.setFraction(-4.75); // -19/4
cout << f1.decimalValue() << endl; // outputs -4.75
fraction f2(4,8); // 4/8
fraction f3(2.875); // 2 7/8
cout << f3 << endl; // outputs 2 7/8 (the default output format is mixed)
cout << f2 << endl; // outputs 4/8
fraction f4(16,8);
cout << f4 << endl; // outputs 2
fraction::outputFormat(improper); // set output format to improper fraction
cout << f4 << endl; // outputs 16/8
cout << f3 << endl; // outputs 23/8
cout << f1 << endl; // outputs -19/4
fraction::outputFormat(decimal); // set output format to decimal value
cout << f3 << endl; // outputs 2.875
cout << f2 << endl; // outputs 0.5
cout << f1 << endl; // outputs -4.75
fraction::outputFormat(mixed); // set output format back to default mixed
cout << f3 << endl; // outputs 2 7/8
f1.setFraction(-4.75);
cout << f1 << endl; // outputs -4 3/4
cout << f2 << endl; // outputs 4/8
fraction f5(12,8);
fraction::outputFormat(improper);
cout << f5 << endl; // outputs 12/8
f2.reduce(); // convert to lowest terms
f5.reduce();
cout << f2 << endl; // outputs 1/2
cout << f5 << endl; // outputs 3/2
f5.setDenominator(16); // change denominator and numerator
// error if numerator is not a whole number
cout << f5 << endl; // outputs 24/16
f5.setDenominator(22);
cout << f5 << endl; // outputs 33/22
f5.setDenominator(5); // output error as numerator would have to be 7.5
cout << f5 << endl; // outputs 33/22 (fraction remained the same)
f1 = f3+f2; // f1 put in lowest terms
cout << f1 << endl; // outputs 27/8
f1 = f3-f2;
cout << f1 << endl;
f1 = f3*f2;
cout << f1 << endl;
f1 = f3/f2;
cout << f1 << endl;
f1 = f3;
cout << f1 << endl; // outputs 23/8
f1 = 7.75;
f1 = f2 = 7.75;
cout << f1 << endl; // outputs 31/4
cout << setw(6) << f1 << setw(6) << f2 << endl; // outputs 31/4 31/4
f0.setFraction(31,4);
if (f0 == f1)
cout << "correct\n";
else
cout << "incorrect\n";
// all relational operators should work for comparing 2 fractions
if (f0 == 7.75)
cout << "correct\n";
else
cout << "incorrect\n";
// all relational operators should work for comparing fraction to double
f0.setFraction(2);
if (f0 < 3)
cout << "correct\n";
else
cout << "incorrect\n";
// all relational operators should work for comparing fraction to int
return 0;
}
fraction.h
#ifndef fraction
#include <iostream>
using namespace std;
enum fracForm {improper, mixed, decimal};
class fraction {
enum fracForm {improper, mixed, decimal};
friend istream& operator>>(istream&, fraction&);
friend ostream& operator<<(ostream&, fraction&);
public:
fraction();
fraction(int, int);
fraction(double);
void setFraction(int, int);
void setFraction(double);
void setDenominator(int);
int getNumerator() const;
int getDenominator() const;
double decimalValue() const;
static void outputFormat(fracForm);
void reduce();
fraction operator+(const fraction&) const;
fraction operator-(const fraction&) const;
fraction operator*(const fraction&) const;
fraction operator/(const fraction&) const;
bool operator==(const fraction&) const;
bool operator!=(const fraction&) const;
bool operator<(const fraction&) const;
bool operator>(const fraction&) const;
bool operator<=(const fraction&) const;
bool operator>=(const fraction&) const;
fraction operator+(const int) const;
fraction operator-(const int) const;
fraction operator*(const int) const;
fraction operator/(const int) const;
bool operator==(const int) const;
bool operator!=(const int) const;
bool operator<(const int) const;
bool operator>(const int) const;
bool operator<=(const int) const;
bool operator>=(const int) const;
fraction operator+(const float) const;
fraction operator-(const float) const;
fraction operator*(const float) const;
fraction operator/(const float) const;
bool operator==(const float) const;
bool operator!=(const float) const;
bool operator<(const float) const;
bool operator>(const float) const;
bool operator<=(const float) const;
bool operator>=(const float) const;
fraction operator+(const double) const;
fraction operator-(const double) const;
fraction operator*(const double) const;
fraction operator/(const double) const;
bool operator==(const double) const;
bool operator!=(const double) const;
bool operator<(const double) const;
bool operator>(const double) const;
bool operator<=(const double) const;
bool operator>=(const double) const;
string mix();
private:
int num, denom;
static fracForm format;
};
#endif
fraction.cpp
#include "fraction.h"
#include <iostream>
#include <algorithm>
fraction::fraction() {
num = 0;
denom = 1;
}
fraction::fraction(int numerator, int denominator) {
setFraction(numerator, denominator);
}
fraction::fraction(double deci) {
setFraction(deci);
}
void fraction::setFraction(int numerator, int denominator){
num = numerator;
denom = denominator;
}
void fraction::setFraction(double deci) {
int decimal = deci;
denom = 1;
while (deci != decimal) {
deci = deci * 10;
denom = denom * 10;
decimal = deci;
}
num = deci;
reduce();
}
void fraction::setDenominator(int denominator) {
int denom2 = denominator;
int proportion = num * denom2;
double newProp = proportion / denom;
int intProp = newProp;
if (newProp != intProp) {
try
{
throw 20;
}
catch (int n)
{
cout << "This does not create a fraction with integers." << endl;
}
}
else {num = newProp; denom = denom2;}
}
int fraction::getNumerator() const {
return num;
}
int fraction::getDenominator() const{
return denom;
}
double fraction::decimalValue() const {
double deci = num/denom;
return deci;
}
void fraction::outputFormat(fracForm fType) {
format = fType;
}
void fraction::reduce() {
num = num / __gcd(num, denom);
denom = denom / __gcd(num, denom);
}
fraction fraction::operator+(const fraction& frac) const {
return fraction(decimalValue() + frac.decimalValue());
}
fraction fraction::operator-(const fraction& frac) const {
return fraction(decimalValue() - frac.decimalValue());
}
fraction fraction::operator*(const fraction& frac) const {
return fraction(decimalValue() * frac.decimalValue());
}
fraction fraction::operator/(const fraction& frac) const {
return fraction(decimalValue() / frac.decimalValue());
}
bool fraction::operator==(const fraction& frac) const {
return decimalValue() == frac.decimalValue();
}
bool fraction::operator!=(const fraction& frac) const {
return decimalValue() != frac.decimalValue();
}
bool fraction::operator<(const fraction& frac) const {
return decimalValue() < frac.decimalValue();
}
bool fraction::operator>(const fraction& frac) const {
return decimalValue() > frac.decimalValue();
}
bool fraction::operator<=(const fraction& frac) const {
return decimalValue() <= frac.decimalValue();
}
bool fraction::operator>=(const fraction& frac) const {
return decimalValue() >= frac.decimalValue();
}
fraction fraction::operator+(const int frac) const {
return fraction(decimalValue() + frac);
}
fraction fraction::operator-(const int frac) const {
return fraction(decimalValue() - frac);
}
fraction fraction::operator*(const int frac) const {
return fraction(decimalValue() * frac);
}
fraction fraction::operator/(const int frac) const {
return fraction(decimalValue() / frac);
}
bool fraction::operator==(const int frac) const {
return decimalValue() == frac;
}
bool fraction::operator!=(const int frac) const {
return decimalValue() != frac;
}
bool fraction::operator<(const int frac) const {
return decimalValue() < frac;
}
bool fraction::operator>(const int frac) const {
return decimalValue() > frac;
}
bool fraction::operator<=(const int frac) const {
return decimalValue() <= frac;
}
bool fraction::operator>=(const int frac) const {
return decimalValue() >= frac;
}
fraction fraction::operator+(const float frac) const {
return fraction(decimalValue() + frac);
}
fraction fraction::operator-(const float frac) const {
return fraction(decimalValue() - frac);
}
fraction fraction::operator*(const float frac) const {
return fraction(decimalValue() * frac);
}
fraction fraction::operator/(const float frac) const {
return fraction(decimalValue() / frac);
}
bool fraction::operator==(const float frac) const {
return decimalValue() == frac;
}
bool fraction::operator!=(const float frac) const {
return decimalValue() != frac;
}
bool fraction::operator<(const float frac) const {
return decimalValue() < frac;
}
bool fraction::operator>(const float frac) const {
return decimalValue() > frac;
}
bool fraction::operator<=(const float frac) const {
return decimalValue() <= frac;
}
bool fraction::operator>=(const float frac) const {
return decimalValue() >= frac;
}
fraction fraction::operator+(const double frac) const {
return fraction(decimalValue() + frac);
}
fraction fraction::operator-(const double frac) const {
return fraction(decimalValue() - frac);
}
fraction fraction::operator*(const double frac) const {
return fraction(decimalValue() * frac);
}
fraction fraction::operator/(const double frac) const {
return fraction(decimalValue() / frac);
}
bool fraction::operator==(const double frac) const {
return decimalValue() == frac;
}
bool fraction::operator!=(const double frac) const {
return decimalValue() != frac;
}
bool fraction::operator<(const double frac) const {
return decimalValue() < frac;
}
bool fraction::operator>(const double frac) const {
return decimalValue() > frac;
}
bool fraction::operator<=(const double frac) const {
return decimalValue() <= frac;
}
bool fraction::operator>=(const double frac) const {
return decimalValue() >= frac;
}
fraction::mix() {
int integer = 0;
int n = num;
while (num > denom) {
num = num - denom;
integer = integer + 1;
}
return to_string(integer) + " " + to_string(num) + "/" + to_string(denom);
}
istream& operator>>(istream& in, fraction& frac) {
in >> frac.num;
char i;
in >> i;
in >> frac.denom;
return i;
}
ostream& operator<<(ostream& out, fraction& frac) {
switch (frac.format) {
case improper:
out << to_string(frac.getNumerator()) + "/" + to_string(frac.getDenominator()); break;
case decimal:
out << frac.decimalValue(); break;
case mixed:
out << frac.mix(); break;
}
}
Here are the specific errors:
main.cpp: In function ‘int main()’:
main.cpp:27:35: error: no matching function for call to ‘fraction::outputFormat(fracForm)’
fraction::outputFormat(improper); // set output format to improper fraction
^
In file included from main.cpp:4:0:
fraction.h:27:21: note: candidate: static void fraction::outputFormat(fraction::fracForm)
static void outputFormat(fracForm);
^~~~~~~~~~~~
fraction.h:27:21: note: no known conversion for argument 1 from ‘fracForm’ to ‘fraction::fracForm’
main.cpp:32:34: error: no matching function for call to ‘fraction::outputFormat(fracForm)’
fraction::outputFormat(decimal); // set output format to decimal value
^
In file included from main.cpp:4:0:
fraction.h:27:21: note: candidate: static void fraction::outputFormat(fraction::fracForm)
static void outputFormat(fracForm);
^~~~~~~~~~~~
fraction.h:27:21: note: no known conversion for argument 1 from ‘fracForm’ to ‘fraction::fracForm’
main.cpp:37:32: error: no matching function for call to ‘fraction::outputFormat(fracForm)’
fraction::outputFormat(mixed); // set output format back to default mixed
^
In file included from main.cpp:4:0:
fraction.h:27:21: note: candidate: static void fraction::outputFormat(fraction::fracForm)
static void outputFormat(fracForm);
^~~~~~~~~~~~
fraction.h:27:21: note: no known conversion for argument 1 from ‘fracForm’ to ‘fraction::fracForm’
main.cpp:44:35: error: no matching function for call to ‘fraction::outputFormat(fracForm)’
fraction::outputFormat(improper);
^
In file included from main.cpp:4:0:
fraction.h:27:21: note: candidate: static void fraction::outputFormat(fraction::fracForm)
static void outputFormat(fracForm);
^~~~~~~~~~~~
fraction.h:27:21: note: no known conversion for argument 1 from ‘fracForm’ to ‘fraction::fracForm’
fraction.cpp:235:15: error: ISO C++ forbids declaration of ‘mix’ with no type [-fpermissive]
fraction::mix() {
^
fraction.cpp:235:1: error: prototype for ‘int fraction::mix()’ does not match any in class ‘fraction’
fraction::mix() {
^~~~~~~~
In file included from fraction.cpp:1:0:
fraction.h:69:16: error: candidate is: std::string fraction::mix()
string mix();
^~~
fraction.cpp: In function ‘std::istream& operator>>(std::istream&, fraction&)’:
fraction.cpp:250:12: error: invalid initialization of reference of type ‘std::istream& {aka std::basic_istream&}’ from expression of type ‘char’
return i;
You declare the enum fracForm two times:
enum fracForm {improper, mixed, decimal};
class fraction {
enum fracForm {improper, mixed, decimal};
The functions in class fraction are now using the enum fraction::fracForm, and the code outside (so in main.cpp) uses fracForm
These two enums aren't the same, so the code won't compile.
To solve the problem, you can use either the "global" approach, where fracForm is resolved from the fraction class:
enum fracForm {improper, mixed, decimal};
class fraction {
//enum fracForm {improper, mixed, decimal};
With that solution, I would think most errors are gone.
The other solution would be, to put the fracForm error in the fraction class scope:
//enum fracForm {improper, mixed, decimal};
class fraction {
public:
enum fracForm {improper, mixed, decimal};
Now you have to use the enum outside the class like:
fracForm type = fraction::improper;

How to define some function which can be accessed from within the scope of operator overloading for (+) operator?

I created a code by using operator overloading concept in c++ for addition of two rational numbers by overloading the +(plus) and the <<(Insertion) operator and I am not able to get a function being accessed from the scope of operator overloading of + operator. How to access the LCM() function from the scope of operator+ overloaded?
I tried using the friend function to access my LCM() function from within the scope of operator+ overloaded function but it didn't work!
#include <iostream>
using namespace std;
class Rational {
private:
int num, den;
public:
int getnum()
{
return this->num;
}
int getden()
{
return this->den;
}
Rational(int num = 0, int den = 0)
{
this->num = num;
this->den = den;
}
int LCM(int a, int b);
friend ostream& operator<<(ostream& out, Rational& r);
friend Rational operator+(Rational x, Rational y);
};
int Rational::LCM(int a, int b)
{
int i = a > b ? a : b;
for (i; i <= a * b; i++) {
if (i % a == 0 && i % b == 0) {
break;
}
}
return i;
}
ostream& operator<<(ostream& out, Rational& r)
{
out << r.getnum() << "/" << r.getden();
return out;
}
Rational operator+(Rational x, Rational y)
{
Rational temp;
int temp1;
int div;
temp1 = LCM(x.den, y.den);
temp.num = (int(temp1 / x.den) * x.num) + (int(temp1 / y.den) * y.num);
temp.den = temp1;
return temp;
}
int main()
{
Rational r1(3, 2);
Rational r2(9, 4);
Rational r3;
r3 = r1 + r2;
cout << r3;
return 0;
}
And I get the error:
error: ‘LCM’ was not declared in this scope
I would say that int Rational::LCM(int a,int b) does not really have anything to do with the Rational class and should probably be a utility function (non-member function), then you would be able to use it anywhere

Redefinition of class in CPP File Error

I know there are plenty of questions like these, but I couldn't find a solution that worked for me.
Okay, I am trying to make simple fraction calculator than can add or subtract any number of functions and write the answer as a reduced fraction.
Example: input=
3/2 + 4/
8
, output =
2
I am trying overload operators in order to accomplish this.
So in the program, I am trying to develop the input consists of an expression made of fractions separated by the operators '+'or '-'.
The number of fractions in the expression is arbitrary.
Each of the following 6 lines is an example of valid input expression:
1/2 + 3/4
1/2 -5/7+3/5
355/113
3 /9-21/ -7
4/7-5/-8
-2/-3+7/5
The problem that I am having is that in when I run my Main CPP program it has a class redefinition error:
fraction.cpp:6:7: error: redefinition of 'Fraction'
class Fraction
^
./Fraction.h:7:7: note: previous definition is here
class Fraction{
However, that should not be a problem because I declared the class and member functions in my header files and defined them in my CPP file.
My following code is below:
Header File
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction{
public:
Fraction(int , int );
int fraction(int,int);
void reduce_fraction(int *, int *);
Fraction& operator+(const Fraction& n);
Fraction& operator-(const Fraction& n);
friend ostream& operator<<(ostream &os, const Fraction& n);
friend istream& operator>>(istream &is, const Fraction& n);
};
#endif
CPP File
#include <iostream>
using namespace std;
#include "Fraction.h"
#include <stdexcept>
class Fraction
{
public:
Fraction::Fraction(int a, int b);
int find_gcd(int n1, int n2);
void reduce_fraction(int *nump, int *denomp)
{
int gcd;
gcd = find_gcd(*nump, *denomp);
*nump = *nump / gcd;
*denomp = *denomp / gcd;
if ((*denomp<0 && *nump < 0 ))
{
*denomp*=-1;
*nump*=-1;
}
else if (*denomp < 0 && *nump >0){
*denomp*=-1;
}
if ( *denomp ==0) {
throw invalid_argument( "Error: zero denominator" );
}
}
Fraction& Fraction::operator+(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) + (n.denom * n.nump);
return Fraction(numera,denom);
}
Fraction& Fraction::operator-(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) - (n.denom* n.nump);
return Fraction(numera, denom);
}
friend ostream& operator<<(ostream &os, Fraction& n)
{
if (n.numera == 0)
{
cout << 0 << endl;
return os;
}
else if (n.numera == n.denom)
{
cout << 1 << endl;
return os
}
else
{
cout << n.numera << '/' << n.denom << endl;
return os;
}
}
friend istream& operator>>(istream &os, Fraction& n)
{
char slash = 0;
return is >> n.numera >> slash >> n.denom;
}
};
#include <iostream>
using namespace std;
#include "Fraction.h"
#include <stdexcept>
class Fraction
{
public:
Fraction::Fraction(int a, int b);
int find_gcd(int n1, int n2);
void reduce_fraction(int *nump, int *denomp)
{
int gcd;
gcd = find_gcd(*nump, *denomp);
*nump = *nump / gcd;
*denomp = *denomp / gcd;
if ((*denomp<0 && *nump < 0 ))
{
*denomp*=-1;
*nump*=-1;
}
else if (*denomp < 0 && *nump >0){
*denomp*=-1;
}
if ( *denomp ==0) {
throw invalid_argument( "Error: zero denominator" );
}
}
Fraction& Fraction::operator+(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) + (n.denom * n.nump);
return Fraction(numera,denom);
}
Fraction& Fraction::operator-(const Fraction& n) {
int denom = *denomp * n.denom;
int numera = (*nump * n.numera) - (n.denom* n.nump);
return Fraction(numera, denom);
}
friend ostream& operator<<(ostream &os, Fraction& n)
{
if (n.numera == 0)
{
cout << 0 << endl;
return os;
}
else if (n.numera == n.denom)
{
cout << 1 << endl;
return os
}
else
{
cout << n.numera << '/' << n.denom << endl;
return os;
}
}
friend istream& operator>>(istream &os, Fraction& n)
{
char slash = 0;
return is >> n.numera >> slash >> n.denom;
}
};
MAIN CPP FILE
#include "Fraction.h"
#include <iostream>
using namespace std;
int main()
{
Fraction x(2,3);
Fraction y(6,-2);
cout << x << endl;
cout << y << endl;
cin >> y;
cout << y << endl;
Fraction z = x + y;
cout << x << " + " << y << " = " << z << endl;
}
I am essentially having trouble with understanding why I am getting the previous definition error and how exactly to fix it. Also, if you see anything else wrong with my implementation in this program I would appreciate you telling me!
Thank you very much!
The way to define member functions in Fraction.cpp is not like this:
class Fraction
{
public:
void reduce_fraction(int *nump, int *denomp)
{
...
}
}
but rather like this:
void Fraction::reduce_fraction(int *nump, int *denomp)
{
...
}
More generally, you should not write this much code before testing any of it.

request for a member in which is of non class type c++

I am striving to test every function in this class and I cannot test the function that will assign a numerator and denominator to the constructor that takes no arguments here is my code. I get this error request for a member 'numerator' in 'first' which is of non class type 'Rational'
#include "Rational.h"
#include <iostream>
#include <string>
#include "GCD.h"
#include <assert.h>
using namespace std;
Rational:: Rational()
{
myNumerator = 1;
myDenominator = 1;
}
Rational:: Rational(int numerator, int denominator)
{
assert(denominator != 0);
myNumerator = numerator;
myDenominator = denominator;
reduce();
}
Rational:: Rational(const Rational &r)
{
myNumerator = r.myNumerator;
myDenominator = r.myDenominator;
}
const Rational& Rational :: operator = (const Rational &rhs)
{
if (this != &rhs)
{
myNumerator = rhs.myNumerator;
myDenominator = rhs.myDenominator;
return rhs;
}
}
int Rational:: numerator(int a) const
{
myNumerator = a;
return myNumerator;
}
int Rational:: denominator(int b) const
{
myNumerator = b;
return myDenominator;
}
void Rational:: reduce()
{
int commonDivisor = GCD(myNumerator, myDenominator);
myNumerator = myNumerator / commonDivisor;
myDenominator = myDenominator / commonDivisor;
}
Rational operator + (const Rational &lhs, const Rational &rhs)
{
int numerator = lhs.numerator() * rhs.denominator() + rhs.numerator() * lhs.denominator();
int denominator = lhs.denominator() * rhs.denominator();
Rational sum(numerator, denominator);
return sum;
}
Rational operator - (const Rational &lhs, const Rational &rhs)
{
int numerator = lhs.numerator() * rhs.denominator() + rhs.numerator() * lhs.denominator();
int denominator = lhs.denominator() * rhs.denominator();
Rational difference(numerator, denominator);
return difference;
}
Rational operator * (const Rational &lhs, const Rational &rhs)
{
int numerator = lhs.numerator() * rhs.numerator();
int denominator = lhs.denominator() * rhs.denominator();
Rational product(numerator, denominator);
return product;
}
Rational operator / (const Rational &lhs, const Rational &rhs)
{
int numerator = lhs.numerator() * rhs.denominator();
int denominator = lhs.denominator() * rhs.numerator();
Rational product(numerator, denominator);
return product;
}
ostream& operator << (ostream & os, const Rational &r)
{
os << r.numerator() << "/" << r.denominator();
return os;
}
istream& operator >> (istream &is, Rational &r)
{
char divisionSymbol;
int numerator = 0, denominator = 0;
is >> numerator >> divisionSymbol >> denominator;
assert(divisionSymbol == '/');
assert(denominator != 0);
Rational number(numerator, denominator);
r = number;
return is;
}
#include <iostream>
#include "Rational.h"
using namespace std;
int main()
{
Rational first(), second(75, 350), third(13, 55);
Rational fourth(second);
cout << first << endl << second << endl << third << endl << fourth <<endl;
first.numerator(3)const;
first.denominator(5)const;
cout << first;
}
Rational first(); is parsed as a function declaration. it is known as Most vexing parse.
You may use Rational first;
And in C++11, you may also use Rational first{};
There is no reason to mark your functions as const if you're going to modify your member variables. It's a promise that you won't modify *this and you do so anyway. So simply remove it. You also provide const "getters" anyway.
int numerator(int a);
int numerator() const {return myNumerator;}
int denominator(int b);
int denominator() const {return myDenominator;}
int Rational:: numerator(int a)
{
// ..
}
int Rational:: denominator(int b)
{
// ..
}
Your copy assignment operator also doesn't make sense. I suggest changing it:
if (this == &rhs)
{
return *this;
}
myNumerator = rhs.numerator();
myDenominator = rhs.denominator();
return *this;
Now for the error others pointed out, your compiler happily warns that empty parentheses interpreted as a function declaration. Simply remove the parentheses or replace them with brackets {} in C++11 mode. Also, you don't need the const keyword when calling a const member function.
Rational first, second(75, 350), third(13, 55);
first.numerator(3);
first.denominator(5);