gpt4 book ai didi

c++ - 谷歌测试 ASSERT_EQ 不编译

转载 作者:行者123 更新时间:2023-11-28 00:42:36 24 4
gpt4 key购买 nike

我有一个类,假设它名为 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> ]

最佳答案

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

你对 ASSERT_EQ 编译错误消息的看法是正确的......但并不是很有帮助......

关于c++ - 谷歌测试 ASSERT_EQ 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18041345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com