Google mock compile error (error: ‘<function name>’ is not a type)
Asked Answered
M

2

14

My actual code (class name changed, some cut, as it is company confidential, but there is only one compiler error, so what I cut should not be affecting things)

class Xyz
{
public:
   virtual void vPrintStatus() const;  
};

and its mock

class MockXyz : public Xyz
{
 public:
    MOCK_CONST_METHOD0(vPrintStatus,
            void());
};

Which gives me a compiler error : error: ‘vPrintStatus’ is not a type

#includes, etc are OK. The compiler is obviously finding vPrintStatus, as, if I change it to something undefined:

MOCK_CONST_METHOD0(independence,
                void());

I get error: ‘independence’ has not been declared.

So, the compiler finds vPrintStatus and appears to know its type (or, at least, what type it is not).

I am sure that I am following the syntax for MOCK_CONST_METHOD0 - the mock macro shoudl be expecting a function name, not a type, as its first parameter.

What am I doing wrong?

Mime answered 16/9, 2015 at 14:59 Comment(4)
Is that real code, copy-pasted from the file you're compiling?Embry
It looks that MOCK_CONST_METHOD0 is parsed as a member function name, not a macro, make sure the includes are correctYourself
Also make sure Xyz has a virtual destructor.Embry
@PiotrSkotnicki - you were correct - there was a missing #include "gmock.h". Feel free to post that as answer. Sorry to waste everyone's time, but I have been banging my head against this for hours :-(Mime
Y
14

The below error message:

error: ‘vPrintStatus’ is not a type

indicates that MOCK_CONST_METHOD0(vPrintStatus, void()); was parsed by a compiler as a declaration of a member function, named MOCK_CONST_METHOD0, taking two parameters, one of type vPrintStatus (hence the error), and another being a function pointer type (void(*)() after adjustment). Clearly, this means that the definition of macro MOCK_CONST_METHOD0 is not visible to the translation unit the mock declaration is part of. Make sure you have included <gmock/gmock.h> to that file.

Yourself answered 16/9, 2015 at 15:38 Comment(2)
FYI: I found that I got this same error when I tried to MOCK_METHOD11. GMock doesn't support more than 10 parameters in a mocked method. See https://mcmap.net/q/831071/-mock-method-with-11-parameters-with-gmock/149212.Ingeminate
Got this error when tried to use StrictMock<MyMock> without using namespace testing; // there are several possible causes for that errorAlpers
H
-1

I had a similar issues and it turns out I was trying to use:

MOCK_CONST_METHODO instead of

MOCK_CONST_METHOD0 (zero works much better the O)

Hohenzollern answered 6/9, 2019 at 17:15 Comment(1)
Get yourself normal font (like Iosevka etc) or syntax aware editor/IDERode

© 2022 - 2024 — McMap. All rights reserved.