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

时间:2017-01-15 22:16:47

标签: c++ mocking gmock

我正在玩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从自动变量转换为指针,我可能有这个complile并且工作,但那不是我想要提供的接口。

我得到的确切错误是:

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));

1 个答案:

答案 0 :(得分:0)

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