GoogleTest PrintTo not getting called for a class

前端 未结 1 1976
悲哀的现实
悲哀的现实 2021-01-11 14:09

I\'m having a rather strange problem telling googletest to print a certain class the way I want using PrintTo.
The class is a very simple 2D point, it is in a namespace

相关标签:
1条回答
  • 2021-01-11 14:43

    Problem is that you break the One Definition Rule (ODR) of one of the gtest function (probably template ::testing::PrintToString<MyNamespace::CPunto2D>(const MyNamespace::CPunto2D&)).

    In one TU where you use ASSERT_EQ, void PrintTo(const MyNamespace::CPunto2D& pto, ::std::ostream* os) is not declared, so ::testing::PrintToString<MyNamespace::CPunto2D> uses the default printer.

    In an other TU where you use ASSERT_EQ, you have void PrintTo(const MyNamespace::CPunto2D& pto, ::std::ostream* os) declared (and potentially defined), so ::testing::PrintToString<MyNamespace::CPunto2D> uses a version using your custom PrintTo.

    That is a second different definition of the same function.

    You have to make sure that each TU which uses ASSERT_EQ see the declaration of your custom PrintTo (as in CPunto2D's header).

    0 讨论(0)
提交回复
热议问题