LearnFrenzy
Sign in Take a free test

Input/Output with cin and cout · Find the Error · Question 10

Error: copying std::cout into a variable Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

void report(std::ostream out) {
    out << "ok\n";
}

int main() {
    report(std::cout);
    return 0;
}
  • A report must return int
  • B Streams cannot be copied (the copy constructor is deleted); pass the stream by reference: std::ostream& out Correct
  • C cout cannot be passed to functions
  • D ok needs std::endl
Correct Answer: Option B

Taking std::ostream& lets the same function write to std::cout, std::cerr, a file or a string stream.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links