gpt4 book ai didi

c++ - 具有负大小的 Google Mock testing::internal::ReturnAction 数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:59:22 26 4
gpt4 key购买 nike

我有测试代码可以做类似的事情

EXPECT_CALL(mock, getSomeString()).WillOnce(Return(&testString));

其中 getSomeString() 通过引用返回:

std:string& getSomeString();

得到

../../../../src/test/unit/gmock/gmock-actions.h: In member function ‘testing::internal::ReturnAction<R>::operator testing::Action<Func>() const [with F = const std::string&(), R = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’:
../../../../src/test/unit/MyTests.cc:148: instantiated from here
../../../../src/test/unit/gmock/gmock-actions.h:467: error: creating array with negative size (‘-0x00000000000000001’)

是什么原因?

最佳答案

检查 gmock-actions.h:467 你会看到:

GMOCK_COMPILE_ASSERT_(
!internal::is_reference<Result>::value,
use_ReturnRef_instead_of_Return_to_return_a_reference);

所以答案是使用 ReturnRef 而不是 Return:

EXPECT_CALL(mock, getSomeString()).WillOnce(ReturnRef(testString));

关于c++ - 具有负大小的 Google Mock testing::internal::ReturnAction<R> 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30323001/

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