<tfoot id='RRlgx'></tfoot>

    <small id='RRlgx'></small><noframes id='RRlgx'>

    <legend id='RRlgx'><style id='RRlgx'><dir id='RRlgx'><q id='RRlgx'></q></dir></style></legend>

    <i id='RRlgx'><tr id='RRlgx'><dt id='RRlgx'><q id='RRlgx'><span id='RRlgx'><b id='RRlgx'><form id='RRlgx'><ins id='RRlgx'></ins><ul id='RRlgx'></ul><sub id='RRlgx'></sub></form><legend id='RRlgx'></legend><bdo id='RRlgx'><pre id='RRlgx'><center id='RRlgx'></center></pre></bdo></b><th id='RRlgx'></th></span></q></dt></tr></i><div id='RRlgx'><tfoot id='RRlgx'></tfoot><dl id='RRlgx'><fieldset id='RRlgx'></fieldset></dl></div>

        <bdo id='RRlgx'></bdo><ul id='RRlgx'></ul>
      1. 重载运算符<<时,std::endl 是未知类型

        std::endl is of unknown type when overloading operatorlt;lt;(重载运算符时,std::endl 是未知类型)
          <bdo id='AjVo9'></bdo><ul id='AjVo9'></ul>
        • <i id='AjVo9'><tr id='AjVo9'><dt id='AjVo9'><q id='AjVo9'><span id='AjVo9'><b id='AjVo9'><form id='AjVo9'><ins id='AjVo9'></ins><ul id='AjVo9'></ul><sub id='AjVo9'></sub></form><legend id='AjVo9'></legend><bdo id='AjVo9'><pre id='AjVo9'><center id='AjVo9'></center></pre></bdo></b><th id='AjVo9'></th></span></q></dt></tr></i><div id='AjVo9'><tfoot id='AjVo9'></tfoot><dl id='AjVo9'><fieldset id='AjVo9'></fieldset></dl></div>
        • <legend id='AjVo9'><style id='AjVo9'><dir id='AjVo9'><q id='AjVo9'></q></dir></style></legend>

                  <small id='AjVo9'></small><noframes id='AjVo9'>

                  <tfoot id='AjVo9'></tfoot>
                    <tbody id='AjVo9'></tbody>

                  本文介绍了重载运算符<<时,std::endl 是未知类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我重载了运算符 <<

                  I overloaded operator <<

                  template <Typename T>
                  UIStream& operator<<(const T);
                  
                  UIStream my_stream;
                  my_stream << 10 << " heads";
                  

                  有效但:

                  my_stream << endl;
                  

                  给出编译错误:

                  错误 C2678:二进制<<": 没有找到采用UIStream"类型的左操作数的运算符(或者没有可接受的转换)

                  error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'UIStream' (or there is no acceptable conversion)

                  制作 my_stream << 的方法是什么?endl 工作吗?

                  推荐答案

                  std::endl 是一个函数,std::cout 通过实现 来利用它运算符<< 使用与 std::endl 具有相同签名的函数指针.

                  std::endl is a function and std::cout utilizes it by implementing operator<< to take a function pointer with the same signature as std::endl.

                  在那里,它调用函数,并转发返回值.

                  In there, it calls the function, and forwards the return value.

                  这是一个代码示例:

                  #include <iostream>
                  
                  struct MyStream
                  {
                      template <typename T>
                      MyStream& operator<<(const T& x)
                      {
                          std::cout << x;
                  
                          return *this;
                      }
                  
                  
                      // function that takes a custom stream, and returns it
                      typedef MyStream& (*MyStreamManipulator)(MyStream&);
                  
                      // take in a function with the custom signature
                      MyStream& operator<<(MyStreamManipulator manip)
                      {
                          // call the function, and return it's value
                          return manip(*this);
                      }
                  
                      // define the custom endl for this stream.
                      // note how it matches the `MyStreamManipulator`
                      // function signature
                      static MyStream& endl(MyStream& stream)
                      {
                          // print a new line
                          std::cout << std::endl;
                  
                          // do other stuff with the stream
                          // std::cout, for example, will flush the stream
                          stream << "Called MyStream::endl!" << std::endl;
                  
                          return stream;
                      }
                  
                      // this is the type of std::cout
                      typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
                  
                      // this is the function signature of std::endl
                      typedef CoutType& (*StandardEndLine)(CoutType&);
                  
                      // define an operator<< to take in std::endl
                      MyStream& operator<<(StandardEndLine manip)
                      {
                          // call the function, but we cannot return it's value
                          manip(std::cout);
                  
                          return *this;
                      }
                  };
                  
                  int main(void)
                  {
                      MyStream stream;
                  
                      stream << 10 << " faces.";
                      stream << MyStream::endl;
                      stream << std::endl;
                  
                      return 0;
                  }
                  

                  希望这能让您更好地了解这些东西是如何工作的.

                  Hopefully this gives you a better idea of how these things work.

                  这篇关于重载运算符<<时,std::endl 是未知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                  <legend id='iV1Ku'><style id='iV1Ku'><dir id='iV1Ku'><q id='iV1Ku'></q></dir></style></legend>

                      <i id='iV1Ku'><tr id='iV1Ku'><dt id='iV1Ku'><q id='iV1Ku'><span id='iV1Ku'><b id='iV1Ku'><form id='iV1Ku'><ins id='iV1Ku'></ins><ul id='iV1Ku'></ul><sub id='iV1Ku'></sub></form><legend id='iV1Ku'></legend><bdo id='iV1Ku'><pre id='iV1Ku'><center id='iV1Ku'></center></pre></bdo></b><th id='iV1Ku'></th></span></q></dt></tr></i><div id='iV1Ku'><tfoot id='iV1Ku'></tfoot><dl id='iV1Ku'><fieldset id='iV1Ku'></fieldset></dl></div>
                        <tbody id='iV1Ku'></tbody>

                        <small id='iV1Ku'></small><noframes id='iV1Ku'>

                      • <tfoot id='iV1Ku'></tfoot>
                          <bdo id='iV1Ku'></bdo><ul id='iV1Ku'></ul>