Is it possible to have class declaration and implementation in same .cpp file?
I want to do some unit-testing with help of mock object. Here is some example of my te
When Base class has any virtual function which is not pure, it's definition needs to be included while compiling the final binary, otherwise it gives linker error for vtable or typeinfo. Look at below example:
// Base.h
struct Base {
virtual void fun() = 0;
virtual ~Base();
};
// Base.cpp
#include"Base.h"
Base::~Base () {}
// Derived.cpp
#include"Base.h"
struct Derived : Base {
void fun () {}
};
int main () {
Derived d;
}
Now compile-link for Derived.cpp and Base.cpp will work fine. Both .cpp files also can be compiled separately for creating object files and then linked together.
From your question, what I feel is that, you are not somehow attaching the .cpp/object file of class AbstractConnection, which still contains one non pure virtual function -- its destructor. If you compile that definition also along with your ConnectionMockup then the linker error should not appear. Either you can compile the file including destructor body or define destructor body in the class definition itself.