gpt4 book ai didi

c++ - 让编译器告诉哪些方法不具体而不是错误 'invalid new-expression of abstract class type'

转载 作者:行者123 更新时间:2023-11-28 01:36:56 26 4
gpt4 key购买 nike

每次我向抽象类添加一个非具体的虚方法时,编译器都会给出无用的错误invalid new-expression of abstract class type '...' on all the classes derived from那个抽象类,然后我需要做 git diff 来搜索我之前添加的新方法,或者在 GCC 的错误中寻找注释。 (添加方法和编译之间可能有天数差异)

我能否在 C++ 中指定一个类必须是具体的(如果它没有说明原因/缺失方法)?

最佳答案

回答手头的问题:

Can I specify in C++ that a class must be concrete (and if it's not tell the cause/missing-method)?

C++ 没有明确的抽象具体 类。这些概念隐含在语言中。 抽象 由一个或多个纯虚拟成员函数的存在(在类本身中或继承)暗示,具体 不存在此类函数。

这正是 C++ 标准定义抽象类的方式 (§13.4/2)

[class.abstract]

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it. A class is abstract if it has at least one pure virtual function.

没有像Java的abstract这样的关键字,比如在C++中。

编译器错误消息是您可以捕获错误的标准位置,例如尝试实例化仍然具有纯虚拟成员函数的类。

关于c++ - 让编译器告诉哪些方法不具体而不是错误 'invalid new-expression of abstract class type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48902408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com