Search code examples
c++mockinggooglemock

gmock call to implicitly deleted copy constructor


I'm playing with gmock, and I have a contrived example I'm using to learn it's nuances. I have a problem with a call to what I would have expected the implicit copy constructor:

// 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);
    ...
};

Now, I could probably have this complile and work if I convert mockWordA and mockWordB from automatic variables to pointers, but that's not the interface I want to provide.

The exact error I'm getting is:

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

Solution

  • I solved the problem by changing my function interface to Strings::addWord(Word* word) which to me seems restrictive given that I'd rather not use pointers with such a basic example.