1

上記のテストフレームワークでタイプを出力したいと思います。 グーグルはそれが可能であると明確に述べています。

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

私はこれをしたようです。しかし、コンパイルしようとすると、次のようになります。

error: no match for ‘operator<<’ in ‘* os << val’ /usr/include/c++/4.4/ostream:108: note: candidates are:

最後に私の過負荷を伴う提案の長いリストが続きoperator<<ます:

std::ostream& Navmii::ProgrammingTest::operator<<(std::ostream&, Navmii::ProgrammingTest::AsciiString&)

誰かが助けることができますか?

4

1 に答える 1

1

operator<<非定数AsciiStringオブジェクトを定義したようです。Googleが印刷しようとしているものは、おそらくconstです。印刷する値を変更するべきではないため、代わりに2番目のパラメーターをconst参照として渡します。

std::ostream& Navmii::ProgrammingTest::operator<<(
  std::ostream&,
  Navmii::ProgrammingTest::AsciiString const&);

これは、リンクされたドキュメントのコードとより厳密に一致します。ただし、その部分は質問の引用から省略されています。

質問は例を引用していPrintToます。そのコードは問題ありませんが、それがあなた自身のコードで実際に行ったことではないと思います。ドキュメントに記載されているように、PrintToを提供したくないoperator<<場合、またはoperator<<クラスのが単体テスト中のデバッグ出力の目的に不適切な場合に使用できます。

于 2012-11-27T16:36:57.430 に答える