【发布时间】:2009-11-09 05:23:18
【问题描述】:
我发现对我来说最耗时的编译器错误之一是“无法实例化抽象类”,因为问题始终是我不打算让类成为抽象类并且编译器没有列出哪些函数是抽象的。必须有一种更智能的方法来解决这些问题,而不是阅读标题 10 次,直到我最终注意到某处缺少“const”。这些是怎么解决的?
【问题讨论】:
-
一些编译器DO提到当实例化错误发生时哪些方法导致类是抽象的。
标签: c++ compiler-errors
我发现对我来说最耗时的编译器错误之一是“无法实例化抽象类”,因为问题始终是我不打算让类成为抽象类并且编译器没有列出哪些函数是抽象的。必须有一种更智能的方法来解决这些问题,而不是阅读标题 10 次,直到我最终注意到某处缺少“const”。这些是怎么解决的?
【问题讨论】:
标签: c++ compiler-errors
无法实例化抽象类
基于这个错误,我猜你正在使用 Visual Studio(因为当你尝试实例化一个抽象类时,Visual C++ 就是这么说的)。
查看 Visual Studio 输出窗口(查看 => 输出);输出应在错误说明后包含一条语句:
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的示例代码给出的错误)
在 Visual Studio 中,“错误列表”窗口仅显示错误消息的第一行。
【讨论】:
C++ 准确地告诉您哪些函数是抽象的,以及它们在哪里声明:
class foo {
virtual void x() const = 0;
};
class bar : public foo {
virtual void x() { }
};
void test() {
new bar;
}
test.cpp: In function ‘void test()’:
test.cpp:10: error: cannot allocate an object of abstract type ‘bar’
test.cpp:5: note: because the following virtual functions are pure within ‘bar’:
test.cpp:2: note: virtual void foo::x() const
所以也许可以尝试使用 C++ 编译您的代码,或者指定您的编译器,以便其他人可以为您的特定编译器提供有用的建议。
【讨论】:
C++Builder 告诉你哪个方法是抽象的:
class foo {
virtual void x() const = 0;
};
class bar : public foo {
virtual void x() { }
};
new bar;
[BCC32 Error] File55.cpp(20): E2352 Cannot create instance of abstract class 'bar'
[BCC32 Error] File55.cpp(20): E2353 Class 'bar' is abstract because of 'foo::x() const = 0'
【讨论】: