I'm having some trouble implementing pure virtual functions inherited from some abstract class, when the classes in question are divided into *.h
and *.cpp
files. The compiler (g++
) tells me that the derived class cannot be instantiated because of the existence of pure functions.
/** interface.h**/
namespace ns
{
class Interface {
public:
virtual void method()=0;
}
}
/** interface.cpp**/
namespace ns
{
//Interface::method()() //not implemented here
}
/** derived.h **/
namespace ns
{
class Derived : public Interface {
//note - see below
}
}
/** derived.cpp **/
namespace ns
{
void Derived::Interface::method() { /*doSomething*/ }
}
/** main.cpp **/
using namespace ns;
int main()
{
Interface* instance = new Derived; //compiler error
}
Does this mean that I have to declare the method() twice - in the Interface's *.h
and in the derived.h
too? Is there no other way around?