title: Passing reference of ostream as an function argument
body: <p>I'm trying to create a function that will take <code>ostream</code> object as an argument, and then print to it. I plan to pass some of my <code>fstream</code> (files), and <code>cout</code>.</p>
  
  <p>This is my code:</p>
  
  <pre><code>void printTask(int op1, int op2, int len, char operation, std::ostream&amp; os = std::cout)
  {
      os &lt;&lt; endl &lt;&lt; "  " &lt;&lt; setw(len) &lt;&lt; op1 &lt;&lt; endl;
      os &lt;&lt; operation &lt;&lt; " " &lt;&lt; setw(len) &lt;&lt; op2 &lt;&lt; endl;
      os &lt;&lt; "-------------" &lt;&lt; endl;
      os &lt;&lt; "  ";
      for(int i = 0; i &lt; len; i++)
      {
          os &lt;&lt; "?";
      }
      os &lt;&lt; endl;
  }
  </code></pre>
  
  <p>From <code>main()</code>, it goes like this...</p>
  
  <pre><code>ofstream output("output.txt", ios::out);
  cout &lt;&lt; addition(xyz, output) &lt;&lt; endl;
  </code></pre>
  
  <p>and that function looks like this:</p>
  
  <pre><code>int addition(int xyz, ofstream file)
  {
  int op1 = ...;
  int op2 = ...;
  char choice = ...;
  
  printTask(op1, op2, xyz, choice, file);
  printTask(op1, op2, xyz, choice, cout); //this cout is not necessary, default parameter
  }
  </code></pre>
  
  <p>I get an error about <code>std::ios::base</code> being private, but I did pass my <code>ostream</code> object by reference so it doesn't try to use copy constructor.</p>
  
  <p>Any ideas?</p>
  
  <p>EDIT: Wanted to update this with an answer. Prototype for <code>addition</code> was wrong, <code>ofstream</code> should be passed by reference, instead of copying it.
  This works:
  <code>int addition(int xyz, ofstream&amp; file) {...}</code></p>
  
  <p>Big thanks to @Jarod42</p>