错误解决方法:“无法实例化抽象类"

2021-12-31 00:00:00 compiler-errors c++

我发现对我来说最耗时的编译器错误之一是无法实例化抽象类",因为问题始终是我不希望类是抽象的,并且编译器没有列出哪些函数是抽象的.必须有一种更聪明的方法来解决这些问题,而不是阅读标题 10 次,直到我终于注意到某处缺少const".你如何解决这些问题?

I find one of the most time-consuming compiler errors for me is "cannot instantiate abstract class," since the problem is always that I didn't intend for the class to be abstract and the compiler doesn't list which functions are abstract. There's got to be a more intelligent way to solve these than reading the headers 10 times until I finally notice a missing "const" somewhere. How do you solve these?

推荐答案

无法实例化抽象类

cannot instantiate abstract class

基于此错误,我猜测您使用的是 Visual Studio(因为当您尝试实例化抽象类时,Visual C++ 就是这样说的).

Based on this error, my guess is that you are using Visual Studio (since that's what Visual C++ says when you try to instantiate an abstract class).

查看 Visual Studio 输出窗口(查看 => 输出);输出应在错误后包含一条语句,说明:

Look at the Visual Studio Output window (View => Output); the output should include a statement after the error stating:

stubby.cpp(10) : error C2259: 'bar' : cannot instantiate abstract class
due to following members:
'void foo::x(void) const' : is abstract
stubby.cpp(2) : see declaration of 'foo::x'

(这是 bdonlan 的示例代码给出的错误)

(That is the error given for bdonlan's example code)

在 Visual Studio 中,错误列表"窗口仅显示错误消息的第一行.

In Visual Studio, the "Error List" window only displays the first line of an error message.

相关文章