所以我试图让它运行但我一直在
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
^
代码:
//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;
};
答案 0 :(得分:0)
您有fraction
个班级和两个fraction
:result
和f1
。您在fraction::AddedTo
上致电f1
并尝试将结果(int
)分配给result
;但是,编译器不知道如何为int
分配单个fraction
。
为此,您需要fraction
的{{3}}:
fraction& operator=(int n)
{
numerator = denominator = n; // or however you wish to handle this
return *this;
}
默认情况下,编译器可以将两个fraction
一起添加,因为它只是猜测您希望执行以下操作:
fraction& operator=(const fraction& f)
{
numerator = f.numerator;
denominator = f.denominator;
return *this;
}
但是对于其他一切,你需要重载运算符。