Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with ostream/ofstream inheritance

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!

like image 757
Daniel Standage Avatar asked Jun 23 '11 16:06

Daniel Standage


2 Answers

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

void print_something(std::ostream& outstream);
like image 89
Cat Plus Plus Avatar answered Oct 10 '22 19:10

Cat Plus Plus


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

void print_something(std::ostream & outstream)
                             //  ^^^  pass by reference.
like image 30
Martin York Avatar answered Oct 10 '22 17:10

Martin York