Tuesday, November 2, 2010

Trying to Copy a std::ostream

I bumped into a particularly cryptic compiler error today...

undefined reference to `std::ios_base::ios_base(std::ios_base const&)'|

After some interesting debugging I came across the problem in my code.

The Problem
void print(std::ostream zout = std::cout) { /* Some Code */ }

Can you spot what the problem is?

Hopefully you noticed that I'm passing zout by value, and std::ostream doesn't have a copy constructor.

The Solution
void print(std::ostream &zout = std::cout) { /* Some Code */ }

If I pass zout as a reference the compiler accepts it without a word and the desired behavior is achieved.

1 comment:

  1. I don't know who you are, but I must tell you your post just saved me. I have been stuck with this linker error for the past two days. Thank you!

    ReplyDelete