0

Let's say we have:

void print(std::ostream &o) {
    o << "Hello World!";
}

and:

int main() {
    std::stringstream ss;
    ss << print(std::cout); // does not work
    std::string outputString = ss.str();
    std::cout << outputString << endl;
}

I understand this will not working because print() has type void. How would I change my code for this to work?

Solution:

int main() {
    std::stringstream ss;
    print(ss);
    std::string outputString = ss.str();
    std::cout << outputString << endl;
}
nasiedlak
  • 19
  • 1
  • 3
  • If "Solution" is meant to be the solution to your question , it should be posted in the Answer box instead of as part of the question – M.M Apr 13 '22 at 02:47
  • Why don't you just [return](https://godbolt.org/z/d5eh5vnd3) the stream reference? – Bob__ Apr 13 '22 at 08:47

0 Answers0