Consider the following three programs in C++:
program 1
struct base{
virtual ~base() =0;
};
struct derived: public base{
~derived();
};
derived::~derived(){}
int main(){}
program 2
struct base{
virtual ~base() =0;
};
struct derived: public base{
~derived(){}
};
int main(){}
program 3
struct base{
virtual void func() =0;
};
struct derived: public base{
void func();
};
void derived::func(){}
int main(){}
The programs #2 and #3 compile and run fine however the first gives the following error:
Undefined symbols for architecture x86_64: "base::~base()", referenced from: derived::~derived() in main-d923b9.o ls: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'd like to know why I am unable to define virtual destructors outside of the class definition however I am able to do it inside the class definition. Additionally I can define methods outside the class, just not destructors.