我是 C++ 的初学者。我想知道为什么在这一行抛出异常:
operator double() const
{
return this->denominator/this->numerator;
}
我找到了numerator == 0
,但我在ctor
. 为什么?请帮我。
class Rational {
public:
int numerator;
int denominator;
Rational(int numerator = 2, int denominator = 1)
{
numerator = 2;
denominator = 1;
};
operator double() const
{
return this->denominator/this->numerator;
};
};
int main(int argc, const char * argv[])
{
Rational r(1, 2);
cout << r;
// double d = 0.5*r;
return 0;
}