Question

I have the following class:

// IVirtualController.hpp
class IVirtualController
{
public:
  virtual InputCode validate(int keycode) = 0;
  virtual ~IVirtualController();
};

And now I want to mock this class:

// InputManagerTest.cpp
class MockController : public IVirtualController
{
public:
  MOCK_METHOD1(validate,InputCode(int keycode));
};

But this doesn't work! I get the following errors:

test/input/InputManagerTest.o: In function MockController::~MockController()': test/input/InputManagerTest.cpp:19: undefined reference toIVirtualController::~IVirtualController()' test/input/InputManagerTest.cpp:19: undefined reference to `IVirtualController::~IVirtualController()'

test/input/InputManagerTest.o: In function IVirtualController::IVirtualController()': test/input/../../src/input/IVirtualController.hpp:14: undefined reference tovtable for IVirtualController'

test/input/InputManagerTest.o: In function MockController::MockController()': test/input/InputManagerTest.cpp:15: undefined reference toIVirtualController::~IVirtualController()'

test/input/InputManagerTest.o:(.rodata._ZTI14MockController[_ZTI14MockController]+0x10): undefined reference to `typeinfo for IVirtualController'

A search told me that "undefined reference to vtable/typeinfo" errors are caused by a missing implementation, a missing "= 0" behind the virtual method declaration or a non-virtual destructor. The class IVirtualController isn't implemented, but that shouldn't be a problem since the MockController is implementing it, right? Commenting out the whole InputManagerTest.cpp mock file "solves" the problem, even though IVirtualController is used in an another class.

How do I test it?

Was it helpful?

Solution

Nope. A destructor must always be implemented. Even a pure virtual one.

That's because of how it works. virtual applied to a destructor holds a different more meaning. Usually, a virtual method isn't called if it's overriden in a derived class, but a virtual destructor will still be called, so an implementation is required.

The =0 for a destructor only makes the class abstract, but, as stated before, an implementation is still required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top