在我的isEmpty函数中,我想检查对象是否处于安全空状态,如果是,则返回true。当denom = -1时,安全空出现,我在函数正上方的构造函数中声明。我该如何访问? 编辑:道歉,我误读了行错误。我想念在不同的线路上使用denom,我修复了错误。抱歉浪费你的时间:(
using namespace std;
namespace sict{
class Fraction{
private:
int num; // Numerator
int denom; // Denominator
int gcd(); // returns the greatest common divisor of num and denom
int max(); // returns the maximum of num and denom
int min(); // returns the minimum of num and denom
public:
void reduce(); // simplifies a Fraction number by dividing the
// numerator and denominator to their greatest common divisor
Fraction(); // default constructor
Fraction(int n , int d=1); // construct n/d as a Fraction number
void display() const;
bool isEmpty() const;
};
};
**实施*
#include "Fraction.h"
using namespace std;
namespace sict
{
Fraction::Fraction()
{
denom =-1; // safe empty state
}
bool Fraction::isEmpty() const
{
//How do I access denom
}
}
答案 0 :(得分:3)
就像这样:
bool Fraction::isEmpty() const
{
return denom == -1;
}