gmock 调用隐式删除的复制构造函数
gmock call to implicitly deleted copy constructor
我正在玩 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);
...
};
现在,如果我将 mockWordA
和 mockWordB
从自动变量转换为指针,我可能可以编译并工作,但这不是我想要提供的接口。
我得到的确切错误是:
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));
我通过将我的函数接口更改为 Strings::addWord(Word* word)
解决了这个问题,这对我来说似乎是限制性的,因为我不想在这样一个基本的例子中使用指针。
我正在玩 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);
...
};
现在,如果我将 mockWordA
和 mockWordB
从自动变量转换为指针,我可能可以编译并工作,但这不是我想要提供的接口。
我得到的确切错误是:
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));
我通过将我的函数接口更改为 Strings::addWord(Word* word)
解决了这个问题,这对我来说似乎是限制性的,因为我不想在这样一个基本的例子中使用指针。