操作员过载以ostream无法与用户定义的类工作
我有一个简单的程序,当我尝试 cout<< 75.0_STC;
我有多个错误,我不知道为什么。只有当我通过参考传递温度对象时,才会发生。
class temperature
{
public:
long double degrees;
temperature(long double c): degrees{c}{}
long double show()const {return degrees;}
};
temperature operator"" _stC(long double t){
return temperature(t);
}
ostream & operator<<(ostream &ekran, temperature &t)
{
ekran << t.show();
return ekran;
}
I have this simple program and when i try to cout << 75.0_stC ;
i have multiple errors and i don't know why.This things only happen when i pass my temperature object via reference.
class temperature
{
public:
long double degrees;
temperature(long double c): degrees{c}{}
long double show()const {return degrees;}
};
temperature operator"" _stC(long double t){
return temperature(t);
}
ostream & operator<<(ostream &ekran, temperature &t)
{
ekran << t.show();
return ekran;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要对要打印的参数进行
const
引用:临时性不会绑定到非const参考参数。
You likely need to take a
const
reference to the argument you'd like to print:Temporary won't bind to the non-const reference argument.