ostream& operator <<(ostream& osObject, const storageRentals& rentals)
{
osObject << rentals.summaryReport();
return osObject;
}
summaryReport()
是一个 void 函数,它给了我一个错误:
没有运算符“
但如果我改变的话错误就不存在summaryReport
函数到int
,但我遇到的问题是你必须返回一个值,并且它正在将其打印在屏幕上。
void storageRentals::summaryReport() const
{
for (int count = 0; count < 8; count++)
cout << "Unit: " << count + 1 << " " << stoUnits[count] << endl;
}
有什么办法可以超载吗cout <<
带有 void 函数?
你应该定义summartReport
taking ostream&
作为参数,如下所示:
std::ostream& storageRentals::summaryReport(std::ostream & out) const
{
//use out instead of cout
for (int count = 0; count < 8; count++)
out << "Unit: " << count + 1 << " " << stoUnits[count] << endl;
return out; //return so that op<< can be just one line!
}
然后将其称为:
ostream& operator <<(ostream& osObject, const storageRentals& rentals)
{
return rentals.summaryReport(osObject); //just one line!
}
顺便说一下,它不叫“超载计算”。你应该说,“超载operator<<
for std::ostream
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)