运算符<<无法输出 std::endl -- 修复吗?

发布于 2024-08-28 07:50:27 字数 1295 浏览 1 评论 0 原文

以下代码在应该仅输出 std::endl 时给出错误:

#include <iostream>
#include <sstream>

struct MyStream {
  std::ostream* out_;
  MyStream(std::ostream* out) : out_(out) {}
  std::ostream& operator<<(const std::string& s) {
    (*out_) << s;
    return *out_;
  }
};

template<class OutputStream>
struct Foo {
  OutputStream* out_;
  Foo(OutputStream* out) : out_(out) {}
  void test() {
    (*out_) << "OK" << std::endl;
    (*out_) << std::endl; // ERROR     
  }
};

int main(int argc, char** argv){
  MyStream out(&std::cout);
  Foo<MyStream> foo(&out);
  foo.test();
  return EXIT_SUCCESS;
}

错误是:

stream1.cpp:19: error: no match for 'operator<<' in '*((Foo<MyStream>*)this)->Foo<MyStream>::out_ << std::endl'
stream1.cpp:7: note: candidates are: std::ostream& MyStream::operator<<(const std::string&)

因此它可以输出字符串(请参见错误上方的行),但不仅仅是 std ::endl,大概是因为 std::endl 不是字符串,但 operator<< 定义要求一个字符串。

模板化 operator<< 没有帮助:

  template<class T>
  std::ostream& operator<<(const T& s) { ... }

如何使代码工作?谢谢!

The following code gives an error when it's supposed to output just std::endl:

#include <iostream>
#include <sstream>

struct MyStream {
  std::ostream* out_;
  MyStream(std::ostream* out) : out_(out) {}
  std::ostream& operator<<(const std::string& s) {
    (*out_) << s;
    return *out_;
  }
};

template<class OutputStream>
struct Foo {
  OutputStream* out_;
  Foo(OutputStream* out) : out_(out) {}
  void test() {
    (*out_) << "OK" << std::endl;
    (*out_) << std::endl; // ERROR     
  }
};

int main(int argc, char** argv){
  MyStream out(&std::cout);
  Foo<MyStream> foo(&out);
  foo.test();
  return EXIT_SUCCESS;
}

The error is:

stream1.cpp:19: error: no match for 'operator<<' in '*((Foo<MyStream>*)this)->Foo<MyStream>::out_ << std::endl'
stream1.cpp:7: note: candidates are: std::ostream& MyStream::operator<<(const std::string&)

So it can output a string (see line above the error), but not just the std::endl, presumably because std::endl is not a string, but the operator<< definition asks for a string.

Templating the operator<< didn't help:

  template<class T>
  std::ostream& operator<<(const T& s) { ... }

How can I make the code work? Thanks!

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

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

发布评论

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

评论(1

浮云落日 2024-09-04 07:50:27

您需要将其添加到您的 struct MyStream 中:

  std::ostream& operator<<( std::ostream& (*f)(std::ostream&) )
  {
      return f(*out_);
  }

std::endl 是一个附加换行符并刷新底层流的函数;此函数签名接受该函数并将其应用于 ostream 成员。

然后,作为测试,定义 foo::test as

  void test() {
    (*out_) << "start";
    (*out_) << std::endl;
    (*out_) << "done";
  }

将正确输出

start
done

You need to add this to your struct MyStream:

  std::ostream& operator<<( std::ostream& (*f)(std::ostream&) )
  {
      return f(*out_);
  }

std::endl is a function that appends a newline and flushes the underlying stream; this function signature accepts that function and applies it to the ostream member.

Then, as a test, defining foo::test as

  void test() {
    (*out_) << "start";
    (*out_) << std::endl;
    (*out_) << "done";
  }

will correctly output

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