Gmock compilation error

0

Good evening, I'm practicing test in c ++ used the gtest / gmock google test library. Everything works fine with gtest, but I can not compile it at all when I try to use gmock. To make sure it's not my problem code, I've decided to copy a sample snippet from library repository.

    #include <gtest/gtest.h>
    #include <gmock/gmock.h>
    class Turtle {

        ~Turtle() = default;

        virtual void PenUp() = 0;

        virtual void PenDown() = 0;

        virtual void Forward(int distance) = 0;

        virtual void Turn(int degrees) = 0;

        virtual void GoTo(int x, int y) = 0;

        virtual int GetX() const = 0;

        virtual int GetY() const = 0;
    };

    class MockTurtle : public Turtle {
    public:

        MOCK_METHOD0(PenUp, void());

        MOCK_METHOD0(PenDown, void());

        MOCK_METHOD1(Forward, void(int distance));

        MOCK_METHOD1(Turn, void(int degrees));

        MOCK_METHOD2(GoTo, void(int x, int y));

        MOCK_CONST_METHOD0(GetX, int());

        MOCK_CONST_METHOD0(GetY, int());
    };
int main(int argc, char *arvg[]) {
    testing::InitGoogleTest(&argc, arvg);
    testing::InitGoogleMock(&argc, arvg);
    return RUN_ALL_TESTS();

}

However, not even this code compiles and I get the following error:

Compiling'main.cpp'.../snap/clion/37/bin/cmake/linux/bin/cmake--build/home/renan/CLionProjects/Testing/cmake-build-debug--targetCMakeFiles/Testing.dir/main.cpp.o---j2-f/home/renan/CLionProjects/Testing/cmake-build-debug/CMakeFiles/Testing.dir/build.make--always-makeBuildingCXXobjectCMakeFiles/Testing.dir/main.cpp.oInfileincludedfrom/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-spec-builders.h:71:0,from/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-generated-function-mockers.h:44,from/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock.h:62,from/home/renan/CLionProjects/Testing/main.cpp:2:/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-matchers.h:842:11:error:invaliduseoftemplate-name‘testing::Matcher’withoutanargumentlistconstMatcherA();^~~~~~~/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-matchers.h:842:11:note:classtemplateargumentdeductionisonlyavailablewith-std=c++1zor-std=gnu++1zInfileincludedfrom/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/internal/gmock-internal-utils.h:45:0,from/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-actions.h:47,from/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock.h:59,from/home/renan/CLionProjects/Testing/main.cpp:2:/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/internal/gmock-generated-internal-utils.h:50:7:note:‘template<classT>classtesting::Matcher’declaredhereclassMatcher;^~~~~~~Infileincludedfrom/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-spec-builders.h:71:0,from/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-generated-function-mockers.h:44,from/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock.h:62,from/home/renan/CLionProjects/Testing/main.cpp:2:/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-matchers.h:Inmemberfunction‘testing::internal::AnythingMatcher::operatortesting::Matcher<A1>()const’:/home/renan/CLionProjects/Testing/lib/googletest-master/googlemock/include/gmock/gmock-matchers.h:1052:40:error:‘A’wasnotdeclaredinthisscopeoperatorMatcher<T>()const{returnA<T>();}

I'dlikesomeonetogivemeinsightintowhatI'mdoingwronginmy tests . Thankful.

    
asked by anonymous 04.09.2018 / 02:20

1 answer

0

After hours of head banging, I solved the problem by simply changing the CMakeLists.txt in the project root of:

cmake_minimum_required(VERSION 3.12)
project(Testing)

set(CMAKE_CXX_STANDARD 14)

add_executable(Testing main.cpp domain/Auction.cpp domain/Auction.h domain/User.cpp domain/User.h domain/Bid.cpp domain/Bid.h service/Evaluator.cpp service/Evaluator.h builder/TestBuilder.cpp builder/TestBuilder.h domain/Payment.cpp domain/Payment.h persistence/FakeAuctionDAO.cpp persistence/FakeAuctionDAO.h persistence/AuctionDAO.cpp persistence/AuctionDAO.h service/Finisher.cpp service/Finisher.h)
add_subdirectory(lib/googletest-master)
include_directories(lib/googletest-master/googletest/include)
include_directories(lib/googletest-master/googlemock/include)
target_link_libraries(Testing gtest gtest_main)

To:

cmake_minimum_required(VERSION 3.12)
project(Testing)
set(CMAKE_CXX_STANDARD 14)

add_subdirectory(lib/googletest-master)
include_directories(lib/googletest-master/googletest/include)
include_directories(lib/googletest-master/googlemock/include)

add_executable(Testing main.cpp domain/Auction.cpp domain/Auction.h domain/User.cpp domain/User.h domain/Bid.cpp domain/Bid.h service/Evaluator.cpp service/Evaluator.h builder/TestBuilder.cpp builder/TestBuilder.h domain/Payment.cpp domain/Payment.h persistence/FakeAuctionDAO.cpp persistence/FakeAuctionDAO.h persistence/AuctionDAO.cpp persistence/AuctionDAO.h service/Finisher.h service/Finisher.cpp)

target_link_libraries(Testing gtest gtest_main)
target_link_libraries(Testing gmock gmock_main)

And that solved my problem.

    
05.09.2018 / 15:25