ostream<<迭代器,C++

发布于 2024-12-08 17:40:18 字数 306 浏览 1 评论 0 原文

我正在尝试构建一个打印列表的运算符,
为什么 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
     }
}

I'm trying to build an operator which prints list,
Why won't ostream<<*it compile?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍景流年 2024-12-15 17:40:18

因为*it没有实现流插入。也就是说,采用 ostreamToperator<< 没有重载。请注意,您应该返回 ostream& os 以允许运算符链接。此外,您的函数模板定义看起来也是错误的。考虑这样做:

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

或者更好的是,支持所有类型的元素和特征上的流:

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

此外,您可以将分隔符传递给 std::ostream_iterator 构造函数以在每个元素之间插入。

* 更新:*
我只是注意到,即使您的函数模板声明是正确的,您也会处理依赖类型。迭代器依赖于类型 T,因此您需要将此告知编译器:

typename list<T>::iterator it;

Because *it does not implement stream insertion. That is, there is no overload for operator<< that takes an ostream and a T. Note that you should be returning the ostream& os to allow operator chaining. 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.

* 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:

typename list<T>::iterator it;
痴情 2024-12-15 17:40:18

我认为问题出在你的模板声明中。以下内容应该可以编译并正常工作:

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

当然,前提是列表的元素类型实际上可以与 ostream<< 运算符一起使用。

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.

星星的轨迹 2024-12-15 17:40:18

您使用模板语法的方式是错误的:

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

顺便说一句,您应该返回对流的引用以允许链接输出运算符,并且列表应该是 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;
}

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:

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;
}
昨迟人 2024-12-15 17:40:18

重写为:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文