我正在尝试定义一个布尔函数,该函数将比较其他两个布尔值并在两个布尔值均为false时返回true。我的代码有什么问题?
//this is the header file
class Fraction
{
public:
//default constructor:
Fraction();
//parameterized constructor:
Fraction(int initNum, int initDen);
int GCD(int a, int b);
int LCM (int a, int b);
Fraction Add(Fraction otherFraction);
Fraction Multiply(Fraction otherFraction);
Fraction Divide(Fraction otherFraction);
void Reduce();
void Write();
bool IsEqual(Fraction otherFraction);
bool IsLessThan(Fraction otherFraction);
bool IsGreaterThan(Fraction otherFraction);
Fraction Reciprocal();
//Getters:
int GetDenominator();
int GetNumerator();
private:
int numerator;
int denominator;
};
//this is the implementation file
#include <iostream>
#include "Fraction.h"
using namespace std;
//default constructor:
Fraction::Fraction()
{
numerator = 0;
denominator = 1;
}
//parameterized constructor:
Fraction::Fraction(int initNum, int initDen)
{
numerator = initNum;
denominator = initDen;
}
void Fraction::Write()
{
cout << numerator << "/" << denominator;
}
int Fraction::GetDenominator()
{
return denominator;
}
int Fraction::GetNumerator()
{
return numerator;
}
int GCD(int a, int b)
{
if (b == 0)
return a;
else
return GCD(b, a%b);
}
int LCM (int a, int b)
{
return (a*b)/GCD(a,b);
}
void Fraction::Reduce()
{
int gcd = GCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
Fraction Fraction::Reciprocal()
{
int num = numerator;
int den = denominator;
int num1 = den;
int den1 = num;
Fraction recFraction(num1, den1);
return recFraction;
}
Fraction Fraction::Multiply(Fraction otherFraction)
{
int num1 = numerator;
int num2 = otherFraction.numerator;
int den1 = denominator;
int den2 = otherFraction.denominator;
int prodNum = (num1 * num2);
int prodDen = (den1 * den2);
Fraction prodFraction(prodNum, prodDen);
prodFraction.Reduce();
return prodFraction;
}
Fraction Fraction::Divide(Fraction otherFraction)
{
Fraction quotient;
Fraction::Multiply(otherFraction.Reciprocal());
return quotient;
}
Fraction Fraction::Add(Fraction otherFraction)
{
int lcm = LCM(denominator, otherFraction.denominator);
int num1 = numerator * (lcm/denominator);
int num2 = otherFraction.numerator * (lcm/otherFraction.denominator);
int newNum = num1 + num2;
Fraction newFraction(newNum, lcm);
newFraction.Reduce();
return newFraction;
}
bool Fraction::IsEqual(Fraction otherFraction)
{
Reduce();
otherFraction.Reduce();
int num1 = numerator;
int num2 = otherFraction.numerator;
int den1 = denominator;
int den2 = otherFraction.denominator;
if (num1 == num2 && den1 == den2)
return true;
else
return false;
}
bool Fraction::IsLessThan(Fraction otherFraction)
{
int lcm= LCM(denominator, otherFraction.denominator);
int num1 = numerator * (lcm/denominator);
int num2 = otherFraction.numerator * (lcm/otherFraction.denominator);
if (num1 < num2)
return true;
else
return false;
}
bool Fraction::IsGreaterThan(Fraction otherFraction)
{
return !IsLessThan(otherFraction) && !IsEqual(otherFraction);
}
已编辑以显示所有实施文件。编译器现在输出以下内容:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\BIRCHP~1\AppData\Local\Temp\ccg3Y7E4.o:fraction.cpp:(.text+0x125): undefined reference to `Fraction::GCD(int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\BIRCHP~1\AppData\Local\Temp\ccg3Y7E4.o:fraction.cpp:(.text+0x25c): undefined reference to `Fraction::LCM(int, int)'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\BIRCHP~1\AppData\Local\Temp\ccg3Y7E4.o:fraction.cpp:(.text+0x344): undefined reference to `Fraction::LCM(int, int)'
collect2.exe: error: ld returned 1 exit status
我已多次检查所有内容,但找不到任何错误。我正在尝试与驱动程序文件一起编译实现文件。
答案 0 :(得分:3)
您声明-
,这意味着一个名为bool IsLessThan(Fraction otherFraction)
的函数,该函数将IsLessThan
作为参数并返回Fraction
。但是,您稍后尝试将其称为bool
,就好像它是不带参数的函数一样。提供一个参数,它将起作用。
对于GCD和LCM,您声明的是Fraction::IsLessThan()
和Fraction::GCD
,但从未定义它们。相反,您定义了两个不相关的全局函数,称为Fraction::LCM
和GCD
,因为您忘记了LCM
。
答案 1 :(得分:0)
我认为您的函数需要参数,请尝试以下操作:
if ((Fraction::IsLessThan(otherFraction) == false) && (Fraction::IsEqual(otherFraction) == false) )
return true;
else
return false;
此外,您在if语句中缺少另一个括号
答案 2 :(得分:0)
从语法上讲,您还需要传递函数期望的其他分数,并且还需要在整个-a
条件周围加上括号。您也不需要一直写if
。不需要:
Fraction::
但是与if ((IsLessThan(otherFraction) == false) && (IsEqual(otherFraction) == false))
和true
的比较是多余的。您可以只写:
false
但这仍然是多余的。您根本不需要if (!IsLessThan(otherFraction) && !IsEqual(otherFraction))
语句。您可以这样做:
if
最后,为了避免在每个函数调用中复制参数,您应该将函数的参数类型从return !IsLessThan(otherFraction) && !IsEqual(otherFraction);
更改为Fraction otherFraction
:
const Fraction& otherFraction
在这种特定情况下,不使用bool Fraction::IsGreaterThan(const Fraction& otherFraction)
{
return !IsLessThan(otherFraction) && !IsEqual(otherFraction);
}
引用不会产生任何开销,因为您的const
类只有两个Fraction
成员。但一般来说,建议您在不将参数存储在某处时传递int
引用。