可能重复:
使用空括号调用默认构造函数时出错
当我运行它时,我得到编译器警告:34 [警告]`Rational test4()的地址,将始终评估为真。但我试图让它默认构造函数是有理数 0/1。第 34 行是 int main() 行:cout << test4;。
#include <iostream>
using namespace std;
class Rational
{
public:
Rational();
friend ostream& operator <<(ostream& out,Rational rational1);
private:
int numerator;
int denominator;
};
int main()
{
//Rational test1(24,6), test2(24);
Rational test4();
//cout << test1<< endl;
//cout << test2<< endl;
cout << test4;
system("pause");
}
Rational::Rational() : numerator(0), denominator(1)
{
//empty body
}
ostream& operator <<(ostream& out,Rational rational1)
{
out << rational1.numerator <<"/"<<rational1.denominator;
return out;
}