Google Mock: Mocked overloaded functions create warning C4373
I realise this is an old question, but since I stumbled upon it myself now, I'd like to share my solution (or at least explanation):
The problem is likely that your declaration has a const parameter, which will be ignored by the compiler. It is the definition that may effectively use const for the parameter.
It's also mentioned now in the google mock faq now that, in order to get rid of the warning, remove const
from the parameter in the function declaration.
In my case I found it still hard since the function implementation was for a templated class inside a header where declaration and definition happen are both done together. Solution to that is probably to disable the warning when including the mocked class' header.
For me (in VS 2010), specifying the const
on primitive type parameters (which I see you also have) caused this behavior. Whenever such existed in the base class function I wanted to override, I couldn't specify the mock in a way so that this warning did not occur; when only having class type const value / const reference parameters, the warning never occured.
So to me it seems like the warning in that case is actually a mistake in the compiler (as the signatures are exactly the same).
If this is new code, you should be fine. The C4373 warning is saying that old versions of Visual Studio violated the standard. From the linked documentation:
Versions of the compiler prior to Visual C++ 2008 bind the function to the method in the base class, then issue a warning message. Subsequent versions of the compiler ignore the const or volatile qualifier, bind the function to the method in the derived class, then issue warning C4373. This latter behavior complies with the C++ standard.
This would only be a problem if you had broken code that relied on Visual Studio's incorrect behavior.
Suggested alternative approach:
#include "stdafx.h"
#include "gmock/gmock.h"
#include "A.h"
class MockA : public A
{
public:
// ...
void myFunc(const int id, const int errorCode, const CString errorMsg) {
mocked_myFunc3(id, errorCode, errorMsg);
}
void myFunc(const CString errorMsg) {
mocked_myFunc1(errorMsg);
}
MOCK_METHOD3(mocked_myFunc_3, void(const int id, const int errorCode, const CString errorMsg));
MOCK_METHOD1(mocked_myFunc_1, void(const CString errorMsg));
// ...
};