gpt4 book ai didi

C++ gmock 在命名空间中找不到方法参数的 operator==

转载 作者:行者123 更新时间:2023-12-04 16:55:24 27 4
gpt4 key购买 nike

我无法使用 gcc 6.4 编译以下代码。
gmock 版本是 1.8.1。
也许 operator== 这里看起来很奇怪,但由于它的大小,我无法附上完整的代码。

#include <iostream>
#include <gmock/gmock.h>

template<class T>
constexpr bool operator==(T&&, T&&) noexcept
{
return true;
}

namespace NNNN
{

struct Param2
{
int i;
};

} // namespace NN

using ::testing::_;
using ::testing::Return;
using ::testing::AtMost;


struct Param
{
int i;
};

struct A
{
MOCK_METHOD1(f, int(const Param&));
};

struct B
{
MOCK_METHOD1(f, int(const NNNN::Param2&));
};


TEST(Test, test)
{
A a;
EXPECT_CALL(a, f(Param{ 1 }));

B b;
std::cout << (NNNN::Param2{ 1 } == NNNN::Param2{ 2 }) << std::endl;
EXPECT_CALL(b, f(NNNN::Param2{ 1 }));
}

int main(int argc, char** argv)
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
我得到的错误:
In file included from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-spec-builders.h:71:0,
from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-generated-function-mockers.h:44,
from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock.h:62,
from /home/jzldfb/projects/uc_workspace/ultracruise/rte/utils/configuration/daemon/sub_module/test/TestApi2.cpp:7:
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h: In instantiation of 'bool testing::internal::AnyEq::operator()(const A&, const B&) const [with A = NNNN::Param2; B = NNNN::Param2]':
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h:1083:18: required from 'bool testing::internal::ComparisonBase<D, Rhs, Op>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = const NNNN::Param2&; D = testing::internal::EqMatcher<NNNN::Param2>; Rhs = NNNN::Param2; Op = testing::internal::AnyEq]'
/home/jzldfb/projects/uc_workspace/ultracruise/rte/utils/configuration/daemon/sub_module/test/TestApi2.cpp:60:1: required from here
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h:238:60: error: no match for 'operator==' (operand types are 'const NNNN::Param2' and 'const NNNN::Param2')
bool operator()(const A& a, const B& b) const { return a == b; }

如果我删除 NNNN 命名空间,则编译成功。
如果我为在其 operator== 中调用模板 operator== 的 Param2 添加模板包装器,则编译成功。

最佳答案

感谢您将我发送到 ADL 页面。
解决方法是添加:

namespace testing {
namespace internal {
using ::operator==;
} }
testing::internal 它是 gmock 中的一个命名空间,其中 operator== 被调用。
上面的代码指示此命名空间在全局命名空间中查找 operator==。

关于C++ gmock 在命名空间中找不到方法参数的 operator==,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64449735/

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