简体   繁体   English

重载<<操作符,用于std :: ostream

[英]Overloading << operator for std::ostream

ostream& operator <<(ostream& osObject, const storageRentals& rentals)
{
    osObject << rentals.summaryReport();
    return osObject;
}

summaryReport() is a void function, and it is giving me an error: summaryReport()是一个无效函数,它给我一个错误:

no operator "<<" matches these operands 没有运算符“ <<”与这些操作数匹配

but the error is not there if I change the summaryReport function to an int , but the problem I have with that is you have to return a value, and it is printing it out on the screen. 但是如果我将summaryReport函数更改为int ,则不会出现错误,但是我遇到的问题是您必须返回一个值,并将其打印在屏幕上。

void storageRentals::summaryReport() const
{
   for (int count = 0; count < 8; count++)
      cout << "Unit: " << count + 1 << "    " << stoUnits[count] << endl;
}

Is there any way to overload cout << with a void function? 有什么方法可以用void函数重载cout <<吗?

You should define summartReport taking ostream& as parameter, as shown here: 您应该使用ostream&作为参数定义summartReport ,如下所示:

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!
}

then call it as: 然后将其称为:

ostream& operator <<(ostream& osObject, const storageRentals& rentals)
{
    return rentals.summaryReport(osObject); //just one line!
}

By the way, it is not called "overloading cout" . 顺便说一句,它不被称为“ overloading cout” You should say, "overloading operator<< for std::ostream . 您应该说, “为std::ostream重载operator<<

There are two things you need to do here. 您需要在这里做两件事。 Make storageRentals::summaryReport() take an std::ostream& (you can default this to std::cout ): storageRentals::summaryReport()设为std::ostream& (您可以将其默认设置std::cout ):

void storageRentals::summaryReport(std::ostream& os) const
{
    for (int count = 0; count < 8; count++)
    {
        os << "Unit: " << count + 1 << "    " << stoUnits[count] << endl;
    }
}

Then call it thus: 然后这样称呼它:

ostream& operator <<(ostream& osObject, const storageRentals& rentals)
{
    rentals.summaryReport(osObject);
    return osObject;
}

Note: An advantage of making storageRentals::summaryReport() take an std::ostream& is that you can pass it an std::ostringstream in your unit-tests and assert that it is providing correct output. 注意:使storageRentals::summaryReport()采用std::ostream&一个优点是,您可以在单元测试中将其传递给std::ostringstream并断言它提供了正确的输出。

If you overload cout , you may make some other place using code hard to understand, or more confusing. 如果您使cout重载,则可能会使使用code地方变得难以理解,或者更加令人困惑。 In fact, you could make your own class to complete the work. 实际上,您可以自己上课以完成工作。 eg make a class class MyPring , and overload its operator << . 例如,使class MyPring ,并重载其operator <<

Your storage report implicitely is always streamed to cout. 您的存储报告总是隐式地流到cout。 Imagine someone calling your function this way and having the rentals on cout instead of the file. 想象有人以这种方式调用您的函数,并在cout而不是文件上放了租用项。

fstream fs("out.txt");
storageRentals rentals;
fs << rentals;

Why don't you stream your class like this: 您为什么不像这样流式播放您的课程:

ostream& operator <<(ostream& osObject, const storageRentals& rentals)
{

  for (int count = 0; count < 8; count++) {
      osObject << "Unit: " << count + 1 << "    " << rentals.stoUnits[count] << endl;
  }
  return osObject;
}

If the stoUnits member is private you need to make the stream function a friend of your storage class. 如果stoUnits成员是私有成员,则需要使流函数成为您的存储类的朋友。

friend ostream& operator<<(ostream& osObject, const storageRentals& rentals);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM