Problem with ostream/ofstream inheritance
Asked Answered
B

2

5

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!

Balkh answered 23/6, 2011 at 16:37 Comment(0)
M
7

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

void print_something(std::ostream& outstream);
Modifier answered 23/6, 2011 at 16:39 Comment(1)
Wow, that was simple. Thanks!Balkh
J
5

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

void print_something(std::ostream & outstream)
                             //  ^^^  pass by reference.
Jahdol answered 23/6, 2011 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.