gpt4 book ai didi

c++ - gmock 调用隐式删除的复制构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:11 29 4
gpt4 key购买 nike

我正在玩 gmock,我有一个人为的例子,我用它来了解它的细微差别。我在调用隐式复制构造函数时遇到问题:

// mock_word.h
class MockWord : Word {
public:
MockWord(const std::string word) : Word(word) {};
MOCK_METHOD0(pigLatinify, std::string(void));
};

// strings.h
template <typename Word>
class Strings {
...
private:
std::vector<Word>* words = new std::vector<Word>();
public:
// This should call the implicit copy constructor
void addWord(const Word word) {
this->words->push_back(word);
};
...
};

// strings_test.cpp
class StringsTest : public ::testing::Test {
protected:
Strings<MockWord>* strings;
public:
virtual void SetUp() {
strings = new Strings<MockWord>();
};
virtual void TearDown() {
delete strings;
};
};

TEST_F(StringsTest, StringIsAllPigLatinifiedNicely) {
MockWord mockWordA("beast");
MockWord mockWordB("dough");

// Set some expectations for the Mock
EXPECT_CALL(mockWordA, pigLatinify()).Times(AtLeast(1));
EXPECT_CALL(mockWordB, pigLatinify()).Times(AtLeast(1));

strings->addWord(mockWordA);
strings->addWord(mockWordB);
...
};

现在,如果我将 mockWordAmockWordB 从自动变量转换为指针,我可能可以编译并工作,但这不是我想要提供的接口(interface)。

我得到的确切错误是:

error: call to implicitly-deleted copy constructor of 'MockWord'
strings->addWord(mockWordA);
^~~~~~~~~
mock_word.h:11:9: note: copy constructor of 'MockWord' is implicitly deleted because field 'gmock0_pigLatinify_11' has a deleted copy constructor
MOCK_METHOD0(pigLatinify, std::string(void));

最佳答案

我通过将我的函数接口(interface)更改为 Strings::addWord(Word* word) 解决了这个问题,这对我来说似乎是限制性的,因为我不想在这样一个基本的例子中使用指针。

关于c++ - gmock 调用隐式删除的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41666941/

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