C++'s pure virtual function implementation and header files
You forgot to declare Derived::method()
.
You tried to define it at least, but wrote Derived::Interface::method()
rather than Derived::method()
, but you did not even attempt to declare it. Therefore it doesn't exist.
Therefore, Derived
has no method()
, therefore the pure virtual function method()
from Interface
was not overridden... and therefore, Derived
is also pure virtual and cannot be instantiated.
Also, public void method()=0;
is not valid C++; it looks more like Java. Pure virtual member functions have to actually be virtual, but you did not write virtual
. And access specifiers are followed by a colon:
public:
virtual void method() = 0;
You have to declare your method in the subclass.
// interface.hpp
class Interface {
public:
virtual void method()=0;
}
// derived.hpp
class Derived : public Interface {
public:
void method();
}
// derived.cpp
void
Derived::method()
{
// do something
}