I study the topic of inheritance in the 2010 textbook workshop. There is an example that I took to study the issue of inheritance, where the base abstract class is used. I created an object from the abstract for the inherited class (correct if it is not right) and it fails me, the compiler stubbornly stands on the fact that there are errors.

Base class AString.h

#pragma once #ifndef ASTRING_H #define ASTRING_H #include<string> class AString { public: virtual ~AString() {} virtual const std::string& GetName() = 0; virtual const std::string& GetVal()const = 0; virtual int GetSize()const = 0; }; 

Inherited SymbString.h

 #pragma once #include <string> #include "AString.h" class SymbString :public AString { public: SymbString(std::string _name) :name(_name) {} SymbString(std::string _name, std::string _val) :name(_name), val(_val) {} const std::string& GetName()const { return name; } const std::string& GetVal()const { return val; } int GetSize()const { return val.size(); } private: std::string name, val; }; 

And the file where you can create objects as much as the user wants:

 .... AString* pNewObj; switch (item) { case 1: pNewObj = new SymbString(name, value);// тут подчеркивает и пишет "использование //объекта абстрактного типа класса "SymbString" не допускается чисто //виртуальная функция функцию "AString::GetName" не имеет оператора переопределения " break; case 2: if (!IsHexStrVal(value)) { cout << "Error!" << endl; return; } pNewObj = new HexString(name, value);// выводит тоже самое только для этого случая break; } 

What the compiler wrote ("using an abstract type object of the" SymbString "class does not allow the purely virtual function" AString::GetName "does not have an override operator") causes misunderstanding what they want from me. Is it SymbString for SymbString and HexString to create objects?

I rewrote the code word by word from the textbook.

It is also not clear what the first const before string in the next line means: virtual const std::string& GetVal()const = 0; what is his role?

  • I think it would be useful to indicate which textbook you are going through, perhaps others will also encounter such a problem. - Egor Smolyakov
  • By the way, #pragma once does the same thing as include guards after it (ifndef-def), so that some of this can be removed. - free_ze
  • @free_ze, in my case did not help, only with the inclusion guards that you described above - Semyon Shelukhin
  • @ Semyon Shelukhin This can not be. What is your compiler? - free_ze
  • one
    @ SemenShelukhin You return type const std :: string &, i.e. constant reference to your class data. If you tried to make the type just std :: string &, then the compiler would scold you, because the method itself is labeled const, i.e. it should not change the state of the object either directly or allow it to be done (via a non-constant link or a non-constant pointer (that is, which indicates something changeable)). - free_ze

1 answer 1

You define a new version of GetName() const (constant), rather than override the abstract GetName()

  • It helped, did not understand that it was necessary to put const for a purely virtual function of an abstract class. Now the linker swears, but that's another story. - Semyon Shelukhin
  • Plus, you need to put something in the header files of the #ifndef ... #define .. #endif .. switches. It will work. - Semyon Shelukhin
  • 3
    @ SemenShelukhin if the compiler supports c ++ 11 , you should use the help of the override specifier. - αλεχολυτ