The following piece of code give me the error
undefined reference to `vtable for Derived'
Code :
#include <iostream>
class base{
public:
base(){}
virtual ~base(){}
virtual void test()
{
}
};
class Derived:public base{
public:
Derived(){}
~Derived(){}
void test();
};
int main() {
base* b = new Derived ();
delete b;
}
which i understand is because the virtual fucntion test
is declared but not defined in class Derived
.
But when i compile with g++ -c file.cpp
which as per this Compile or assemble the source files, but do not link. It does not give me any errors and compiles fine. Hence the above error is generated at linking time and not compile time.
From what i learned wasn't the vtable
created at compile time. Then why do i not get the error at compile time itself?