【问题标题】:Overloading << operator for std::ostream为 std::ostream 重载 << 运算符
【发布时间】:2012-04-24 03:33:07
【问题描述】:
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;
}

有没有办法用 void 函数重载cout &lt;&lt;?

【问题讨论】:

    标签: c++ overloading


    【解决方案1】:

    你应该定义summartReport,以ostream&amp;为参数,如下所示:

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

    顺便说一下,它不叫“重载cout”。你应该说,“为std::ostream重载operator&lt;&lt;。

    【讨论】:

      【解决方案2】:

      如果您超载cout,您可能会使使用code 的其他地方难以理解,或者更加混乱。实际上,您可以创建自己的班级来完成工作。例如创建一个类class MyPring,并重载它的operator &lt;&lt;。

      【讨论】:

        【解决方案3】:

        您的存储报告总是隐式地流式传输到 cout。 想象一下有人以这种方式调用您的函数,并将租金放在 cout 而不是文件上。

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

        你为什么不这样流式传输你的课程:

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

        如果 stoUnits 成员是私有的,您需要使流函数成为您的存储类的朋友。

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

        【讨论】:

          【解决方案4】:

          您需要在这里做两件事。使storageRentals::summaryReport() 采用std::ostream&amp;(您可以默认将此设置为std::cout):

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

          然后这样称呼它:

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

          注意:使storageRentals::summaryReport() 采用std::ostream&amp; 的一个优点是您可以在单元测试中将std::ostringstream 传递给它,并断言它提供了正确的输出。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-03
            • 2013-10-10
            • 2017-04-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-17
            • 2020-02-10
            相关资源
            最近更新 更多