0
我已經編寫了以下用於修改分數對象的類。否定分數,但不能修改原始分數
#include "Fraction.h"
#include "GCD.h"
#include <iostream>
using std::cout;
//Implementation of the timesEq() member function
//Performs similar operation as the *= operator on the built-in types
const Fraction & Fraction::timesEq(const Fraction & op)
{
numerator *= op.numerator;
denominator *= op.denominator;
simplify(); // will make sure that denominator is positive and
// will invoke gcd() function to reduce fraction
// as much as possible
return (*this); // returns the object which invoked the method
}
const Fraction & Fraction::plusEq (const Fraction & op)
{
numerator *= op.denominator;
numerator += op.numerator * denominator;
denominator *= op.denominator;
simplify(); // will make sure that denominator is positive and
// will invoke gcd() function to reduce fraction
// as much as possible
return (*this); // returns the object which invoked the method
}
const Fraction & Fraction::minusEq (const Fraction & op)
{
numerator *= op.denominator;
denominator = denominator * op.denominator;
numerator -= op.numerator;
simplify(); // will make sure that denominator is positive and
// will invoke gcd() function to reduce fraction
// as much as possible
return (*this); // returns the object which invoked the method
}
const Fraction & Fraction::divideEq (const Fraction & op)
{
numerator *= op.denominator;
denominator *= op.numerator;
simplify(); // will make sure that denominator is positive and
// will invoke gcd() function to reduce fraction
// as much as possible
return (*this); // returns the object which invoked the method
}
Fraction Fraction::negate(void) const
{
return (*this * -1);
}
void Fraction::display(void)const {
cout << numerator << "/" << denominator;
}
void Fraction::simplify(void)
{
gcd = gcd(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
但我在negate
函數有問題。 我正在使用像這樣的函數:B = A.negate()
,因此,我無法修改原始的A
對象,但需要將否定對象指定給B
。
現在我有實現是給了一個錯誤:
Error: no operator "*" matches these operands
operand types are: const Fraction * int
我不知道我做錯了。什麼需要改變?
如果你'negate'函數有問題,你爲什麼寫了這麼多行代碼,有幾個與'negate'沒有關係? – nhgrif
我只是想確保有足夠的信息來了解我的問題 – theintellects