Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cannot instantiate abstract class

Tags:

c++

I am new to C++. Could you pls help me get rid of the errors:

error C2259: 'MinHeap' : cannot instantiate abstract class

IntelliSense: return type is not identical to nor covariant with return type "const int &" of overridden virtual function function

template <class T> class DataStructure { 
    public:
        virtual ~DataStructure () {}

        virtual bool IsEmpty () const = 0;    

        virtual void Push(const T&) = 0;

        virtual const T& Top() const = 0;

        virtual void Pop () = 0;
};

class MinHeap : public DataStructure<int>
{
    private:
        std::vector<int> A;       

    public:
        bool IsEmpty() const
        {
            ..
        }

        int Top() const
        {
           ..         
        }

        void Push(int item)
        {
            ...
        }

        void Pop()
        {
            ..
        }   
};
like image 427
softwarematter Avatar asked Feb 26 '23 05:02

softwarematter


1 Answers

The problem is with const T& Top() vs. int Top(). The latter is different from the former, and thus not an override. Instead it hides the base class function. You need to return exactly the same as in the base class version: const int& Top() const.
The same problem exists for Push(), BTW.

like image 113
sbi Avatar answered Mar 08 '23 05:03

sbi