ostream/ofstream 继承问题

发布于 2024-11-16 21:25:20 字数 2214 浏览 3 评论 0原文

我正在编写一个 C++ 程序,我需要一些帮助来理解错误。

默认情况下,我的程序打印到终端(STDOUT)。但是,如果用户提供文件名,程序将打印到该文件。如果我要写入终端,我将使用 std::cout 对象,而如果我要写入文件,我将创建并使用 std::ofstream< /代码> 对象。

但是,我不想不断检查我是否应该写入终端或文件。由于 std::cout 和 std::ofstream 对象都继承自 std::ostream 类,我想我会创建一种print_output 函数接受 std::ostream 对象。在调用此函数之前,我会检查是否应该打印到文件。如果是这样,我将创建 std::ofstream 对象并将其传递给打印函数。如果没有,我将简单地将 std::cout 传递给打印函数。这样,打印函数就不必担心打印到哪里。

我认为这是一个好主意,但我无法编译代码。我在这里创建了一个过于简化的示例。这是代码......

#include <fstream>
#include <iostream>
#include <stdio.h>

void print_something(std::ostream outstream)
{
  outstream << "All of the output is going here\n";
}

int main(int argc, char **argv)
{
  if(argc > 1)
  {
    std::ofstream outfile(argv[1]);
    print_something(outfile);
  }
  else
  {
    print_something(std::cout);
  }
}

这是编译时错误。

dhrasmus:Desktop standage$ g++ -Wall -O3 -o test test.c 
/usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.2.1/iosfwd:55: error: within this context
/usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/iosfwd:64: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
test.c: In function ‘int main(int, char**)’:
test.c:15: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here 
test.c:15: error:   initializing argument 1 of ‘void print_something(std::ostream)’

关于为什么我会收到这些错误有什么想法吗?我的代码是否有错误,或者我的方法有根本性的错误吗?

谢谢!

I'm writing a C++ program and I need some help understanding an error.

By default, my program prints to the terminal (STDOUT). However, if the user provides a filename, the program will print to that file. If I'm writing to the terminal, I will use the std::cout object, whereas if I'm writing to a file I'll create and use a std::ofstream object.

However, I don't want to have to continually check whether I'm supposed so to be writing to the terminal or a file. Since both std::cout and std::ofstream objects inherit from the std::ostream class, I figured I would create a sort of print_output function that accepts a std::ostream object. Before calling this function, I would check to see whether I'm supposed to print to a file. If so, I'll create the std::ofstream object and pass it to the print function. If not, I'll simply pass std::cout to the print function. The print function then does not have to worry about where it's printing to.

I thought this was a good idea, but I cannot get the code to compile. I have created an over-simplified example here. Here is the code...

#include <fstream>
#include <iostream>
#include <stdio.h>

void print_something(std::ostream outstream)
{
  outstream << "All of the output is going here\n";
}

int main(int argc, char **argv)
{
  if(argc > 1)
  {
    std::ofstream outfile(argv[1]);
    print_something(outfile);
  }
  else
  {
    print_something(std::cout);
  }
}

...and here is the compile-time error.

dhrasmus:Desktop standage$ g++ -Wall -O3 -o test test.c 
/usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.2.1/iosfwd:55: error: within this context
/usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/iosfwd:64: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
test.c: In function ‘int main(int, char**)’:
test.c:15: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here 
test.c:15: error:   initializing argument 1 of ‘void print_something(std::ostream)’

Any ideas as to why I'm getting these errors? Did I code something wrong, or is there something fundamentally wrong with my approach?

Thanks!

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

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

发布评论

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

评论(2

我一向站在原地 2024-11-23 21:25:20

流不可复制,因此您不能按值将它们传递给函数。请改用参考。

void print_something(std::ostream& outstream);

Streams are not copyable, so you cannot pass them to a function by value. Use a reference instead.

void print_something(std::ostream& outstream);
余厌 2024-11-23 21:25:20

您无法复制流。因此,您需要通过引用传递:

void print_something(std::ostream & outstream)
                             //  ^^^  pass by reference.

You can not copy stream. As a result you need to pass by reference:

void print_something(std::ostream & outstream)
                             //  ^^^  pass by reference.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文