More information on “cannot instantiate abstract class”

≯℡__Kan透↙ 提交于 2020-01-04 06:02:10

问题


Sometimes I am working with relatively complex (and sometimes confusing - with the way they are laid out by whoever wrote it originally) abstract classes. When inheriting from it, I sometimes encounter cannot instantiate abstract class and most of the time it is because I forgot to declare & implement a pure virtual function. Can I get more information from the compiler about which function it found I did not implement instead of hunting for it?


回答1:


Are you using Visual Studio? If so, then switch from Error List tab to Output tab. There will be something like:

main.cpp(8): error C2259: 'foo' : cannot instantiate abstract class
          due to following members:
          'void Foo::method(char)' : is abstract



回答2:


Whenever you encounter that message, then it immediately means that you have not defined a pure virtual function in the derived class, and you want to create an instance of it. And if you're using a good compiler then, I'm sure, it indicates which pure virtual function you didn't implement. At least, GCC indicates that.

See the error message here: http://www.ideone.com/83iDk

prog.cpp: In function ‘int main()’:
prog.cpp:11: error: cannot declare variable ‘a’ to be of abstract type ‘A’
prog.cpp:6: note: because the following virtual functions are pure within ‘A’:
prog.cpp:7: note: virtual void A::f()

That is more than enough that you didn't implement A::f().



来源:https://stackoverflow.com/questions/6295114/more-information-on-cannot-instantiate-abstract-class

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