Fraction 객체를 수정하기 위해 다음 클래스를 작성했습니다.분수를 제외하고 원본을 수정하지 않음
#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