简体   繁体   English

ostream的<

[英]ostream<<Iterator, C++

I'm trying to build an operator which prints list, 我正在尝试构建一个打印列表的运算符,
Why won't ostream<<*it compile? 为什么不会ostream << *它编译?

void operator<<(ostream& os, list<class T> &lst)
{
     list<T>::iterator it;
     for(it = lst.begin(); it!=lst.end(); it++)
     {
                  os<<*it<<endl; //This row
     }
}

Because *it does not implement stream insertion. 因为*it没有实现流插入。 That is, there is no overload for operator<< that takes an ostream and a T . 也就是说, operator<<没有超载,它带有ostreamT Note that you should be returning the ostream& os to allow operator chaining. 请注意,您应该返回ostream& os以允许操作员链接。 Also your function template definition looks wrong. 您的函数模板定义也看起来不对。 Consider doing this instead: 考虑这样做:

template< typename T >
ostream& operator<<(ostream& os, list<T> const& lst)
{
    std::copy(
        lst.begin(), lst.end()
      , std::ostream_iterator< T >( os )
    );
    return os;
}

or better yet, to support streams over all kind of elements and traits: 或者更好的是,支持所有元素和特征的流:

template< typename Elem, typename Traits, typename T >
std::basic_ostream< Elem, Traits >& operator<<(
    std::basic_ostream< Elem, Traits >& os
  , std::list<T> const& lst
)
{
    std::copy(
        lst.begin(), lst.end()
      , std::ostream_iterator< T >( os )
    );
    return os;
}

Adittionaly, you can pass a separator to std::ostream_iterator constructor to be inserted between each element. Adittionaly,您可以将分隔符传递给std::ostream_iterator构造函数,以便在每个元素之间插入。

* Update: * I just noticed that even if your function template declaration were correct, you would be dealing with a dependent type. *更新:*我刚刚注意到即使你的函数模板声明是正确的,你也会处理一个依赖类型。 The iterator is dependent on the type T , so you need to tell this to the compiler: 迭代器依赖于类型T ,因此您需要告诉编译器:

typename list<T>::iterator it;

I think the problem is in your template declaration. 我认为问题出在你的模板声明中。 The following should compile and work just fine: 以下应编译并正常工作:

template <typename T>
void operator<<(ostream& os, list<typename T> &lst)
{
      list<T>::iterator it;
      for(it = lst.begin(); it!=lst.end(); it++)
      {
                  os<<*it<<endl;
      }
}

This is provided of course that the element type of your list can actually be used with the << operator of an ostream . 当然,这提供了列表的元素类型实际上可以与ostream<<运算符一起使用。

You are using template syntax the wrong way: 您正在以错误的方式使用模板语法:

template<class T>
void operator<<(ostream& os, list<T> &lst)
{
    list<T>::iterator it;
    for(it = lst.begin(); it!=lst.end(); it++)
    {
        os<<*it<<endl; //This row
    }
}

And by the way, you should return a reference to the stream to allow chaining of output operators, and the list should be const, and you can also use the standard library for doing the output loop: 顺便说一下,你应该返回对流的引用以允许链接输出操作符,列表应该是const,你也可以使用标准库来执行输出循环:

template<class T>
std::ostream& operator<<(std::ostream& os, const std::list<T> &lst)
{
    std::copy(lst.begin(), lst.end(), std::ostream_iterator<T>(os, "\n"));
    return os;
}

Rewrite to: 重写为:

template<class T>
ostream& operator<<(ostream& os, list<T>& lst){
    typename list<T>::iterator it;
    for(it = lst.begin(); it != lst.end(); ++it){
                 os << *it << endl;
    }
    return os;
}

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

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