I have an abstract base class Foo
with an abstract method called bar
.
I'm calling bar
from Foo
's constructor. I'm expecting subclasses to override bar
(it is abstract after all) and then Foo
to call the overridden bar
on every instance:
class Foo
{
public:
Foo() { bar(); }
virtual void bar() = 0;
}
However, I'm getting an error:
foo.obj:-1: error: LNK2019: unresolved external symbol "protected: virtual void __cdecl Foo::bar(void)" (?bar@Foo@@MEAAXXZ) referenced in function "public: __cdecl Foo::Foo(void)" (??0Foo@@QEAA@XZ)
This kind of linker errors usually means that I've defined something but haven't declared it (or the other way around), and this seems to be the case again.
Everything works just fine if I add a definition for bar
like so:
void Foo::bar() {}
Is this intended error, or is it a bug in the linker? If it's intentional, why so? I can't see why couldn't I call abstract method from constructor? Only reason I think of is that base class' constructor gets called before subclass' methods get defined, but I still don't think I should get this error?
I'm using Qt Creator 2.7.0 - Based on Qt 5.0.2 (32 bit)