繁体   English   中英

如何在一个类中比较布尔值?

[英]How do I compare boolean values in a class?

我正在尝试定义一个布尔函数,该函数将比较其他两个布尔值,如果它们都为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

我已多次检查所有内容,但找不到任何错误。 我正在尝试与驱动程序文件一起编译实现文件。

您声明bool IsLessThan(Fraction otherFraction) ,这意味着一个名为IsLessThan的函数,该函数将Fraction作为参数并返回bool 但是,您稍后尝试将其称为Fraction::IsLessThan() ,就好像它是不带参数的函数一样。 提供一个参数,它将起作用。

至于GCD和LCM,您声明的是Fraction::GCDFraction::LCM但从未定义它们。 相反,您定义了两个不相关的全局函数,称为GCDLCM ,因为您忘记了Fraction::

我认为您的函数需要参数,请尝试以下操作:

 if ((Fraction::IsLessThan(otherFraction) == false) && (Fraction::IsEqual(otherFraction) == false) )
    return true;
 else
     return false;

另外,您在if语句中缺少另一个括号

从句法上讲,您还需要传递函数期望的其他分数,并且还需要在整个if条件中加上括号。 您也不需要一直写Fraction:: 不需要:

if ((IsLessThan(otherFraction) == false) && (IsEqual(otherFraction) == false))

但是,对truefalse比较是多余的。 您可以这样写:

if (!IsLessThan(otherFraction) && !IsEqual(otherFraction))

但这仍然是多余的。 您根本不需要if语句。 您可以这样做:

return !IsLessThan(otherFraction) && !IsEqual(otherFraction);

最后,为了避免在每个函数调用中复制参数,您应该将函数的参数类型从Fraction otherFractionconst Fraction& otherFraction

bool Fraction::IsGreaterThan(const Fraction& otherFraction)
{
    return !IsLessThan(otherFraction) && !IsEqual(otherFraction);
}

在这种特定情况下,不使用const引用不会产生任何开销,因为您的Fraction类只有两个int成员。 但是通常,建议您在不将参数存储在某处时通过const引用传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM