我正在研究一个简单的C程序,并且很难理解我遇到的编译器错误.这个问题是由我尝试从基类创建派生类引起的.我在下面发布了相同结构的代码,但更改了名称.
BaseClass.h
#ifndef BASECLASS_H #define BASECLASS_H class BaseClass { public: BaseClass(void); virtual int method1(void) = 0; virtual int method2(void) = 0; virtual float method3(void) = 0; }; #endif // BASECLASS_H
DerivedClass.h
#ifndef DERIVEDCLASS_H #define DERIVEDCLASS_H #include "DerivedClass.h" class DerivedClass: public BaseClass { public: DerivedClass(void); }; #endif // DERIVEDCLASS_H
DerivedClass.cpp
#include "DerivedClass.h" DerivedClass::DerivedClass(void) { } int DerivedClass::method1(void) { // TODO } int DerivedClass::method2(void) { // TODO } float DerivedClass::method3(void) { // TODO }
no 'int DerivedClass::methodX()' member function declared in class 'DerivedClass'
一旦我在’DerivedClass.h’中声明这些方法,错误就会消失,因为编译器现在知道这些方法.
但是,我很困惑.为什么有必要在DerivedClass.h中重新声明纯虚函数?当我#include DerivedClass.h时,它将自动包含BaseClass.h,因此我假设我的DerivedClass.cpp应该完全了解这些方法.我做错了吗?