У меня есть упражнение дело с классами в C++, в котором я создаю файловую систему, как так (File.h
)Реализация виртуальных функций в сфере наследования
class File {
public:
virtual string getName();
virtual void print() const=0;
virtual bool operator==(const File& file) const=0;
}
Тогда я реализую getName
в File.cpp
и создать TextFile.h
class TextFile : public File {
public:
void print() const;
void operator==(const TextFile& textFile) const;
private:
string text_;
Реализовать в TextFile.cpp
void TextFile :: print() const {
cout << text_ << endl;
}
bool TextFile :: operator==(const TextFile& textFile) const {
return true; //just for compiling
}
при компиляции мы получаем:
$ g++ -Wall -g File.cpp TextFile.cpp -o RunMe
TextFile.cpp: In function ‘int main()’:
TextFile.cpp:8:11: error: cannot declare variable ‘Ilan’ to be of abstract type ‘TextFile’
TextFile Ilan("Ilan", NULL, "Blah \n NewLine");
^
In file included from TextFile.cpp:1:0:
TextFile.h:8:7: note: because the following virtual functions are pure within ‘TextFile’:
class TextFile: public File
^
In file included from TextFile.h:4:0,
from TextFile.cpp:1:
File.h:57:18: note: virtual bool File::operator==(const File&) const
virtual bool operator==(const File& file) const = 0;
я, вероятно, не знаю, как хорошо работать с наследованием и операторскими функциями (видящий функция print
работает хорошо), но я не могу найти проблему при просмотре мой материал курса.
Не ясно ли сообщение об ошибке? Вы должны реализовать 'operator == (const File & file) const', а не' operator == (const TextFile & file) const'. –
Возможный дубликат [оператор виртуального присваивания C++] (http://stackoverflow.com/questions/669818/virtual-assignment-operator-c) –
Я рассмотрел этот вопрос. Не удалось решить мою проблему ... –