While there are other questions on stack overflow which deal with the 'undefined reference to vtable' error message. The following code either compiles or doesn't compile depending on whether the no-args constructor C() is implemented in-line or not. I know that the member function m() should be pure virtual and that would be the correct change to make in order to fix the issue. What is confusing to me is that it can be made to compile with an apparently unrelated change.
The following code does not compile with g++ (4.6.3 on ubuntu 64 bit) and produces the expected 'undefined reference to vtable for C' message (which for the record is still a terrible error message considering the problem is with m())
Header.h
#ifndef HEADER_H
#define HEADER_H
class C
{
public:
C();
virtual void m();
};
#endif
Implementation.cpp
#include "Header.h"
C::C() {}
Main.cpp
#include "Header.h"
int main()
{
return 0;
}
The following unrelated changes allows for compilation:
- Remove non in-lined implementation for C::C() from Implementation.cpp
- Add trivial in-line implementation for C() to class in Header.h
Why does this allow for compilation? Is this a compiler bug, optimizer issue or dark corner of the standard suprise?