問題のクラスがファイルに分割されている場合、抽象クラスから継承された純粋仮想関数の実装に問題が*.hあり*.cppます。コンパイラ ( g++) は、純粋な関数が存在するため、派生クラスをインスタンス化できないことを示しています。
/** 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
}
これは、method() を 2 回宣言する必要があることを意味しますか? インターフェイス*.hと同様にderived.h? 他に方法はありませんか?