gpt4 book ai didi

c++ - 为什么我的默认构造函数返回一个 boolean 值?

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:27 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Error on calling default constructor with empty set of brackets

当我运行它时,我收到编译器警告:34 [Warning] the address of`Rational test4(), will always evaluate as true.但我正在努力使默认构造函数是有理数 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;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com