-1

so I'm trying to get this to run but i keep getting

result = f1.AddedTo(f2); //a class binary operation - a value-returning "observer" function
~~~~~~ ^ ~~~~~~~~~~~~~~
note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'int' to 'const fraction' for 1st argument
class fraction
      ^

Code:

//client.cpp
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
fraction f1(9,8); //calling a parameterized class constructor
fraction f2(2,3);
fraction result; //calling a default class constructor
const fraction f3(12, 8);
const fraction f4(202, 303);
fraction f5,f6;

cout<<"C++ CLASS MULTI-FILE PROJECT"<<endl;
cout<<"Client.cpp - testing a fraction class implementation\n";
cout<<"----------------------------------------------------\n\n";

cout << "The result object starts off at ";
result.showFraction(); //calling a void "observer" function
cout << endl;


cout<<"\nArithmetic operations with fraction objects stored in the results       class object\n";
cout<<"------------------------------------------------------------------------------\n\n";

cout << "The sum of ";
f1.showFraction();
cout << " and ";
f2.showFraction();
cout << " is ";
result = f1.AddedTo(f2); //a class binary operation - a value-returning               "observer" function
result.showFraction();
cout << endl;

cout << "The difference of ";
f1.showFraction();
cout << " and ";
f2.showFraction();
cout << " is ";
result = f1.Subtract(f2); //a class binary operation - a value-returning     "observer" function
result.showFraction();
cout << endl;

cout << "The product of ";
f1.showFraction();
cout << " and ";
f2.showFraction();
cout << " is ";
result = f1.MultipliedBy(f2); //a class binary operation - a value-returning     "observer" function
result.showFraction();
cout << endl;

result = f3.DividedBy(f4); //a class binary operation - a value-returning     "observer" function
cout << "The quotient of ";
f3.showFraction();
cout << " and ";
f4.showFraction();
cout << " is ";
result.showFraction();
cout << endl;

cout<<"\nInput and Output operations for two new class objects\n";
cout<<"-----------------------------------------------------\n\n";

f5.getFraction(); //a class input operation - a transformer or setter function
f5.showFraction(); //a class output operation - an observer or getter     function
cout<<endl<<endl;
f6.getFraction();
f6.showFraction();
cout<<endl<<endl;

cout<<"\nA Boolean operation comparing two class objects\n";
cout<<"------------------------------------\n\n";

if (f5.isGreaterThan(f6)){ //a class relational expression - boolean         operation/function
f5.showFraction();
cout <<" is greater than ";
f6.showFraction();
cout<<endl;
} else {
f5.showFraction();
cout <<" is less than ";
f6.showFraction();
cout<<endl;
}

cout<<"\n---------------------------------------------------------\n";
cout<<"\nFraction class implementation test now successfully concluded\n";

// system ("PAUSE");

return 0;
}

//fraction.cpp
#include <iostream>
#include "fraction.h"

using namespace std;

fraction::fraction()
    {
        numerator = 0;
        denominator = 1;
    }

//---------------------------------------------------------

fraction::fraction(int n, int d)
    {
        numerator = n;
        if (d==0)
        {
            cout << "The denominator can not equal zero. Please try again." << endl;
            exit(0);
        }
        else
            denominator = d;
    }

//---------------------------------------------------------

void fraction::getFraction()
{
    cout << "Plese enter the numerator ";
    cin >> n;
    cout << "Plese enter the denominator ";
    cin >> d;


}

//---------------------------------------------------------


void fraction::showFraction() const
{
    cout <<  numerator << '/' <<  denominator;
}

//---------------------------------------------------------

    // I got the GCD algorithm from the following source:
    // Source C#: http://www.ww.functionx.com/csharp2/examples/gcd.htm
    int gcd(int n, int d)
    {
        int remainder;
        while (d != 0)
        {
            remainder = n % d;
            n = d;
            d = remainder;
        }
        return n;
    }

//---------------------------------------------------------


int fraction::AddedTo(fractionotherFraction )
    {
         n =     numerator*otherFraction.denominator+otherFraction.numerator*denominator;
         d = denominator*otherFraction.denominator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }

//---------------------------------------------------------


int fraction::Subtract(fraction otherFraction)
    {
         n = numerator*otherFraction.denominator-    otherFraction.numerator*denominator;
         d = denominator*otherFraction.denominator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }

//---------------------------------------------------------


int fraction::MultipliedBy(fraction otherFraction)
    {
         n = numerator*otherFraction.numerator;
         d = denominator*otherFraction.denominator;
         return Fraction(n/gcd(n,d),d/gcd(n,d));
    }

//---------------------------------------------------------

int fraction::DividedBy(fraction otherFraction)
    {
         n = numerator*otherFraction.denominator;
         d = denominator*otherFraction.numerator;
        return Fraction(n/gcd(n,d),d/gcd(n,d));
    }

//---------------------------------------------------------

    bool fraction::isGreaterThan(fraction otherFraction)
{

}

//fraction.h
#include <iostream>
using namespace std;

class fraction
{
public:

    fraction();
    fraction(int,int);
    void getFraction();
    void showFraction() const;
    int AddedTo(fraction);
    int Subtract(fraction);
    int MultipliedBy(fraction);
    int DividedBy(fraction) const;
    bool isGreaterThan(fraction);

private:
    int numerator;
    int denominator;

};
lurker
  • 56,987
  • 9
  • 69
  • 103
Lawson Wei
  • 11
  • 1
  • 2
    _Why_? I'll let you know once I read your 400-some lines of code. You could make it easier by providing a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve). As a stab in the dark though: because your `fraction` class **doesn't** have an overloaded `operator=` so the compiler doesn't know how you expect to add a `fraction` and an `int` (returned by `AddedTo`) together. You need to define a custom `operator=` – Tas Nov 13 '15 at 02:59
  • Why do `AddedTo`, `Subtract`, etc. all have `int` as a return type, but `return Fraction(…);`? What is `Fraction` anyway? Your type is `fraction`. – chris Nov 13 '15 at 03:01

1 Answers1

0

You have a fraction class and two fractions: result and f1. You are calling fraction::AddedTo on f1 and trying to assign the result (an int) to result; however, the compiler doesn't know how to assign a single int to your fraction.

For that, you need to overload operator= for your fraction:

fraction& operator=(int n)
{
    numerator = denominator = n; // or however you wish to handle this
    return *this;
}

By default, the compiler would be able to add two fractions together, because it just guesses that you wish to do the following:

fraction& operator=(const fraction& f)
{
    numerator = f.numerator;
    denominator = f.denominator;
    return *this;
}

But for everything else, you need to overload operators.

Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51