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

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

Related

Getting an error, but don't understand why

I have a class called Fraction, and I am trying to reduce fractions before printing them on screen.I have a function called redue() that tries to do that, but somehow this is not working. Here's what I have:
// Fraction.cpp
// Implemenation for Fraction header file
//
#include <iostream>
#include <iomanip>
#include "Fraction.h"
#include <stdexcept>
using namespace std;
// Constructor: empty
Fraction:: Fraction(void) {}
// Construcotr: Intializing numerator and denomenator
Fraction:: Fraction( int a, int b)
{
if (b == 0)
{
throw invalid_argument("Error: division by zero");
}
if(b != 0)
{
reduce(a,b);
setNumo(a);
setDeno(b);
}
}
// Overloading operators
ostream &operator<<(ostream &output, const Fraction &fraction)
{
if( fraction.numo == fraction.deno)
{ output<<1;
return output; }
if (fraction.deno == 1)
{ output <<fraction.numo;
return output;}
if((fraction.numo >=0) && (fraction.deno > 0))
{
output <<fraction.numo <<"/"<<fraction.deno;
return output; // prints a/b
}
}
// insertion operator overloading
istream &operator>>( istream &input, Fraction &fraction)
{
int n1,n2;
input >> n1;
input >> fraction.ch;
input >> n2;
Fraction temp(n1, n2);
fraction.numo = temp.numo;
fraction.deno = temp.deno;
return input;
}
// = operator
const Fraction& Fraction::operator=(const Fraction &f)
{
numo = f.numo;
deno = f.deno;
return *this;
}
Fraction Fraction::operator-(const Fraction &fraction) const {
Fraction temp;
int numo1, numo2;
temp.numo = numo * fraction.deno - fraction.numo * deno;
temp.deno = deno * fraction.deno;
Fraction::reduce(temp.numo, temp.deno);
return temp;
}
Fraction Fraction::operator+(const Fraction &fraction) const {
Fraction temp;
temp.numo = numo * fraction.deno + fraction.numo * deno;
temp.deno = deno * fraction.deno;
Fraction::reduce(temp.numo, temp.deno);
return temp;
}
void Fraction:: setNumo(int a)
{ numo = a; }
void Fraction:: setDeno(int b)
{ deno = b; }
static void Fraction::reduce(int &num1, int &num2)
{
int gcd;
gcd = find_gcd(num1, num2);
if(num2 != 0)
{
num1 = num1/gcd;
num2 = num2/gcd;
}
}
int Fraction::find_gcd(int num1, int num2) // returns gcd of two numbers
{
int gcd, remainder;
remainder = num1 % num2;
while( remainder !=0)
{
num1 = num2;
num2 = remainder;
remainder = num1 % num2;
}
gcd = num2;
return gcd;
}
Here is the error that I get when I try to compile this file:
Fraction.cpp:102:50: error: cannot declare member function ‘static void Fraction::reduce(int&, int&)’ to have static linkage [-fpermissive]
static void Fraction::reduce(int &num1, int &num2)
^
Fraction.cpp: In static member function ‘static void Fraction::reduce(int&, int&)’:
Fraction.cpp:105:30: error: cannot call member function ‘int Fraction::find_gcd(int, int)’ without object
gcd = find_gcd(num1, num2);
Can someone please tell me what the problem is and how I can fix this problem? I really need help. Thank you in advance.
static void Fraction::reduce(int &num1, int &num2)
{
int gcd;
gcd = find_gcd(num1, num2); <<<<<<<<<<This is a member function
if(num2 != 0)
{
num1 = num1/gcd;
num2 = num2/gcd;
}
}
When you declare function as static compiler does not insert this pointer in that function call that means you cannot call any other member function directly inside some static function nor can you use data members of class.
I think your two error messages are pretty clear:
Fraction.cpp:102:50: error: cannot declare member function ‘static void Fraction::reduce(int&, int&)’ to have static linkage [-fpermissive]
static void Fraction::reduce(int &num1, int &num2)
This means you've declared Fraction::reduce(int, int) as a member function (presumably in Fraction.h) but now when you're implementing it, you're trying to make it static. Remove the static and this will be fine.
Fraction.cpp: In static member function ‘static void Fraction::reduce(int&, int&)’:
Fraction.cpp:105:30: error: cannot call member function ‘int Fraction::find_gcd(int, int)’ without object
gcd = find_gcd(num1, num2);
This flows on from the first error. static member functions in C++ are functions of the type, not an instance of that type. You've made reduce static but find_gcd non-static. So reduce is executing as a class function without a this pointer; when it calls find_gcd, it doesn't know which instance of Fraction to use as the this pointer. Making reduce non-static will fix this too.

It says "No matching constructor for initialization of 'Fraction'

.cpp
//
// calculator.cpp
//
#include "Fraction.h"
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction'
char op;
try
{
cin >> x;
cin >> op;
while ( cin && ( op == '+' || op == '-' ) )
{
cin >> y;
if ( op == '+' )
x = x + y;
else
x = x - y;
cin >> op;
}
cout << x << endl;
}
catch ( invalid_argument& e )
{
cout << "Error: " << e.what() << endl;
}
}
.h
#ifndef Fraction_Calculator_Fraction_h
#define Fraction_Calculator_Fraction_h
#include<iostream>
#include<cstdlib>
//Fraction class definition
class Fraction
{
public:
Fraction (int a, int b);
int fraction(int a, int b);
void set(int, int);
int get_numerator(void);
int get_denomenator(void);
int find_gcd (int n1, int n2);
void reduce_fraction(int nump, int denomp);
Fraction& operator+(const Fraction& n);
Fraction& operator-(const Fraction& n);
friend std::ostream& operator<<(std::ostream &os, const Fraction& n);
friend std::istream& operator>>(std::istream &is, Fraction& n);
Fraction& operator= (const Fraction& n);
int denom;
int numera;
private:
int numerator;
int denomenator;
int denomp;
int nump;
};
#endif
It says "No matching constructor for initialization of 'Fraction' on the first line of the cpp file
I don't understand what it means.
The problem is that your Fraction constructor takes 2 arguments.
Fraction (int a, int b);
and you are invoking it with none
Fraction x,y; //ERROR IS RIGHT HERE. It says "No matching constructor for initialization of 'Fraction'
You should either invoke x and y with the 2 int parameters or define another constructor that takes no arguments.
Provide default constructor like
Fraction()
{
numerator=0;
denomenator0;
denomp0;
nump=0;
}

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);

calculate polynomial with coefficients as abstract class Number

I am struggling with adding operations of classes Natural, Rational, Complex that represent appropriate math objects. I need that to calculate polynomial in x.
All classes inherit abstract class Number. Having all coefficients in an array of Numbers I want to calculate the polynomial. To do so I need operation of multiplying by double (x is double). x gets transformed into Rational and multiplied. This works fine. My problem is how to add classes of abstract type Number?
I can't make it work. All I get is never ending recursion in Number::add(Number) (it invokes itself instead of invoking others methods for types Natural, Rational, Complex).
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Natural;class Rational;class Complex;
class Number {
public:
virtual string toString() const = 0;
virtual Number *operator*(const Rational) const = 0;
virtual Number *add(const Natural*) const = 0;
virtual Number *add(const Rational*) const = 0;
virtual Number *add(const Complex*) const = 0;
virtual Number *add(const Number *n) const {
n->add(this);
}
};
class Natural : public Number {
friend class Complex;
int n;
public:
Natural(const Natural &s) {
n = s.n;
}
Natural(int number) : n(number) {}
string toString() const {
stringstream ss;
ss << n;
return ss.str();
}
operator Rational() const;
operator Complex() const;
operator int() const {
return n;
}
Number *operator*(const Rational r) const;
Number *add(const Natural* number) const {
return new Natural(n + number->n);
}
Number *add(const Rational*) const;
Number *add(const Complex*) const;
};
class Rational : public Number {
friend class Natural;
int numerator, denominator;
void divideByGCD() {
int a = numerator, b = denominator;
//cout << a << ' ' << b << ' ';
if(a < b) {
int temp = a;
a = b;
b = temp;
}
while (b > 0) {
int r = a % b;
a = b; b = r;
//cout << r << endl;
}
numerator /= a;
denominator /= a;
//cout << a << endl;
}
public:
Rational() {}
Rational(const Rational &s) {
numerator = s.numerator;
denominator = s.denominator;
}
Rational(int n, int d) {
if(d == 0) throw new runtime_error("denominator equals 0");
if(d < 0) {
numerator = -n;
denominator = -d;
} else {
numerator = n;
denominator = d;
}
divideByGCD();
}
Rational(double d) {
int i = 0, mul = 1;
int r = d-floor(d);;
while(r!=0) {
i++; mul *= 10;
r = 10*r-floor(10*r);
}
numerator = (int)mul*d;
denominator = mul;
divideByGCD();
}
string toString() const {
stringstream ss;
ss << numerator;
if(denominator > 1) ss << '/' << denominator;
return ss.str();
}
operator const Complex() const;
operator const double() const {
return (double)numerator/denominator;
}
Number *operator*(const Rational r) const {
return new Rational(numerator*r.numerator, denominator*r.denominator);
}
Number *add(const Rational* r) const {
return new Rational(numerator*r->denominator+r->numerator*denominator, denominator*r->denominator);
}
Number *add(const Natural*) const;
Number *add(const Complex*) const;
};
class Complex : public Number {
friend class Rational;
double real, imaginary;
static const double radius = 10;
public:
Complex() {}
Complex(const Complex &s) {
real = s.real;
imaginary = s.imaginary;
}
Complex(const double r, const double im) : real(r), imaginary(im) {}
string toString() const {
stringstream ss;
ss << real;
if(imaginary != 0) ss << '+' << imaginary << 'i';
return ss.str();
}
Number *operator*(const Rational r) const;
Number *add(const Complex* c) const {
return new Complex(real + c->real, imaginary + c->imaginary);
}
Number *add(const Natural*) const;
Number *add(const Rational*) const;
};
Natural::operator Rational() const {
return Rational(n,1);
}
Natural::operator Complex() const {
return Complex(n, 0);
}
Rational::operator const Complex() const {
return Complex((double)numerator/denominator, 0);
}
Number *Natural::operator*(const Rational r) const {
return new Rational(n*r.numerator, r.denominator);
}
Number *Complex::operator*(const Rational r) const {
return new Complex(real*(double)r, imaginary*(double)r);
}
Number *Natural::add(const Rational *r) const {
if(r->denominator == 1) return new Natural(n+r->numerator);
else return new Rational(n*r->denominator,r->denominator);
}
Number *Natural::add(const Complex *c) const {
return c->add(this);
}
Number *Rational::add(const Natural *n) const {
return n->add(this);
}
Number *Rational::add(const Complex *c) const {
return new Complex(c->real+(double)*this, c->imaginary);
}
Number *Complex::add(const Natural *number) const {
return new Complex(real+number->n, imaginary);
}
Number *Complex::add(const Rational *r) const {
return r->add(this);
}
Number *poly(double x, Number *a[], unsigned int size) {
if(size == 1) return a[0];
else return a[0]->add((*poly(x, a+1, size-1))*Rational(x));
}
int main() {
cout << (Natural(5)*(Rational)2.0)->toString() << endl;
Number *coefs[] = {new Natural(5), new Natural(6)};
cout << poly(2, coefs, 2) << endl;
}
How should I fix Number::add(Number) so that while invoking add on object of type Number program itself figure out which of virtual method add to choose?
This is known as multi-dispatch. Here are some links to look at
Multiple_dispatch
best multimethods implementation
I think the problem is:
virtual Number *add(const Number *n) const {
n->add(this);
}
If you multiply a Rational by a Natural that is stored in a Number *, it can't polymorphicly upcast the Number * to a Natural *. I agree w/g-makulik in that references/values make a lot more sense here, as you are leaking memory all over the place. Remove support for Number + Number. Also, if I add a Natural and a Rational together, I get a Number * back, but what kind of number is it? I think the architecture needs a bit more thought; I might get rid of the base class pure virtual methods entirely (except maybe toString). For example:
class Number
{
public:
virtual string toString() = 0;
};
class Rational : public Number
{
string toString() {...}
// forget 'add', use operators
Rational operator+(const Rational & _rhs) const {Rational ret; ...; return ret;}
Rational & operator+=(const Rational & _rhs) const {...; return *this;}
...
}
Edit
For a quick fix, I think you just need to get rid of virtual Number *operator*(const Rational) const = 0;, and replace it with a version for each sub-class (e.x., Rational * operator*(const Natural) const)
Or, add an enumerated member variable to Number to keep track of the type:
enum Type { NATURAL, RATIONAL, ...}
Type mType;
or use RTTI, such that you can selectively choose the right add method in Number::add:
Number * add(Number * _rhs)
{
if(_rhs->mType == RATIONAL)
return this->add((Rational *)_rhs);
...
}
it looks kinda sloppy, but it will work
it looks like Visitor pattern is what I've been looking for. I wanted to have functions accept and visit in the same class. I believe my mistake was to give them the same name.