I would like to output my types in the mentioned testing framework. Google clearly states that it’s possible.
As mentioned earlier, the printer is extensible. That means you can teach it to do a better job at printing your particular type than to dump the bytes. To do that, define << for your type:
namespace foo {
class Bar { ... };
// It's important that PrintTo() is defined in the SAME
// namespace that defines Bar. C++'s look-up rules rely on that.
void PrintTo(const Bar& bar, ::std::ostream* os) {
*os << bar.DebugString(); // whatever needed to print bar to os
}
} // namespace foo
I seem to have done this. But when trying to compile I’m getting the following:
error: no match for ‘operator<<’ in ‘* os << val’ /usr/include/c++/4.4/ostream:108: note: candidates are:
It is followed by long list of proposals with my overloaded operator<< at the end:
std::ostream& Navmii::ProgrammingTest::operator<<(std::ostream&, Navmii::ProgrammingTest::AsciiString&)
Can somebody help?
You appear to have defined
operator<<for non-constAsciiStringobjects. Whatever Google is trying to print is probably const. Pass the second parameter as a const reference instead since you shouldn’t be modifying the value you print:That more closely matches the code from the linked documentation. That part is omitted from the quotation in the question, though.
The question quotes the
PrintToexample. That code is fine, but I don’t think that’s what you’ve really done in your own code. As the documentation says, you can usePrintToif you don’t want to provideoperator<<, or if theoperator<<for your class is inappropriate for the purposes of debug output during unit tests.