0

我有一个类,假设它被命名为Foo我没有定义一个相等运算符并且我不想定义一个(出于我自己的原因)。

我想测试一些在 Foo 上运行的函数,我编写了以下代码:

inline bool operator==(const Foo& left, const Foo& right)
{
   // here  I test my equality condition....
}

TEST(SomeTestCase, SomeTest)
{
    Foo expected = ...
    Foo actual = ...

    ASSERT_EQ(expected, actual);     // does NOT compile
    ASSERT_TRUE(expected == actual); // compiles without a problem
}

有谁知道我怎样才能使 ASSERT_EQ 编译,以便在失败的情况下打印一个有意义的错误消息?

我正在使用 MSVC2012,错误消息是:

1>D:\3rdpartycache\CPP\gmock\1.6.0-2\sdk\gtest\include\gtest/gtest.h(1316): error C2784: 'bool testing::internal::operator ==(T *,const testing::internal::linked_ptr<T> &)' : could not deduce template argument for 'T *' from 'const Foo'
1>          D:\3rdpartycache\CPP\gmock\1.6.0-2\sdk\gtest\include\gtest/internal/gtest-linked_ptr.h(213) : see declaration of 'testing::internal::operator =='
1>          D:\3rdpartycache\CPP\gmock\1.6.0-2\sdk\gtest\include\gtest/gtest.h(1353) : see reference to function template instantiation 'testing::AssertionResult testing::internal::CmpHelperEQ<T1,T2>(const char *,const char *,const T1 &,const T2 &)' being compiled
1>          with
1>          [
1>              T1=Foo,
1>              T2=Foo
1>          ]
1>          OperationsOnFooTest.cpp(146) : see reference to function template instantiation 'testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare<Foo,T>(const char *,const char *,const T1 &,const T2 &)' being compiled
1>          with
1>          [
1>              lhs_is_null_literal=false,
1>              T=Foo,
1>              T1=Foo,
1>              T2=Foo
1>          ]
1>          OperationsOnFooTest.cpp(146) : see reference to function template instantiation 'testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare<Foo,T>(const char *,const char *,const T1 &,const T2 &)' being compiled
1>          with
1>          [
1>              lhs_is_null_literal=false,
1>              T=Foo,
1>              T1=Foo,
1>              T2=Foo
1>          ]
4

1 回答 1

3

尝试验证您的 operator== 和 Foo 类是否在同一个命名空间中。我遇到了以这种方式解决的类似错误。

虽然您对 ASSERT_EQ 编译错误消息是正确的......但并不是很有帮助......

于 2014-04-01T06:23:38.603 回答