Why did I get the error “error C2259: … cannot instantiate abstract class”?

独自空忆成欢 提交于 2019-12-02 09:31:37

To fix the specific problem, you need to declare a destructor for ElectricGuitarComponentFactory because you have declared the destructor of the base class as a pure virtual function. Why you have declared the base class destructor as pure virtual, I don't know; it really doesn't make any sense to do that. The destructor should be declared virtual, but not as pure virtual.

Also, the syntax you have used,

public: virtual ~GuitarComponentFactory() = 0 {}

is ill-formed. You cannot declare a pure virtual function and provide a definition for it in the class definition. If you want to provide a definition for a pure virtual function, you must do so outside of the class definition. The compiler you are using, Visual C++, is a bit lenient with respect to this rule.

class GuitarComponentFactory 
  {
     public: virtual ~GuitarComponentFactory() = 0 {}
  };

In your derived ElectricGuitarComponentFactory you don't provide a destructor, hence it's still an abstract class.

ElectricGuitarComponentFactory inherits GuitarComponentFactory ....

When you inherit an abstract class (abstract = has pure virtual methods), then you need to override all those methods.

in GuitarComponentFactory you got 7 methods, and in ElectricGuitarComponentFactory you only declared 6.

You forgot to override the destructor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!