Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6064115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:43+00:00 2026-05-23T09:14:43+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:14:43+00:00Added an answer on May 23, 2026 at 9:14 am

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

    void print_something(std::ostream& outstream);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.