C++ 重载运算符的设置精度<&书信电报;
我让接线员超负荷工作了C++ 重载运算符的设置精度<&书信电报;,c++,C++,我让接线员超负荷工作了 我重载了操作符,只需将其链接:
我重载了操作符,只需将其链接:
<如果你真的是指253
(这是(int)p.code
的值)也应该消失,就不要输出它。哈哈,我解释得不好。“我刚才说的只是钱的问题。”路易泽杜尔多夫说。我更新了答案,给出了一些额外的理由,说明为什么out.setprecision(2)
不起作用。
ostream& operator<< (ostream &out, const Product& p) {
return out << '\t' << (int)p.code << "\tR$ " << p.price << '\t' << p.name;
}
cout << this->items[i] << endl;
ostream& operator<< (ostream &out, const Product& p) {
return out << '\t' << (int)p.code << "\tR$ "
<< std::fixed << std::setprecision(2)
<< p.price << '\t' << p.name;
}
struct _Setprecision { int _M_n; };
// This is what's actually called (both functions below):
inline _Setprecision
setprecision(int __n) { return { __n }; }
template<typename _CharT, typename _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
{
// Note you can alternatively call std::ostream::precision() function
__os.precision(__f._M_n);
return __os;
}