C++ 构造函数错误

标签 c++ constructor compiler-errors abstract-class declaration

<分区>

这是我的代码的一部分: .h 文件中的声明:

virtual bool operator==(const File& file) const = 0;

在 .cpp 文件中

bool File::operator==(const File& file) const {
    return true;
}

我得到这个编译错误:

 1>c:\users\talw\desktop\hw5\hw5\project1\main.cpp(76): error C2259: 
              'Directory' : cannot instantiate abstract class
 1>          due to following members:  
 1>          'bool File::operator ==(const File &) const' : is abstract
 1>          c:\users\talw\desktop\hw5\hw5\project1\file.h(57) : see  
              declaration of 'File::operator =='

请帮忙。 谢谢

最佳答案

这是一个纯虚拟运算符重载,使您的类成为抽象类,因此您可以在同一个类中实现函数,但不能实例化一个抽象类,这会给您带来以下错误。

cannot instantiate abstract class

抽象File类的派生类可以实现该功能并可以被实例化。

关于C++ 构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951277/

相关文章:

qt - QT Powershell错误Windows-build-qt-static.ps1

c++ - 为什么我不能直接在临时对象上调用 operator() ?

c++ - 为什么 std::pair 的大小与其元素的大小之和不同?

c++ - 在 QT 中以全屏模式播放视频

c++ - 如何从 Qt 中的另一个类访问变量?

java - LinkedList 数据结构,为什么我们需要从不同的构造函数调用空的默认构造函数

c++ - 多态运算符[]实现

c++ - 为什么我会调用 implicitly_deleted 默认构造函数?

php - 在 PHP 中,如何反序列化为当前对象?

c# - 为什么此处的Visual Studio无法解决List?