C ++无法实例化抽象类

时间:2010-09-20 09:39:01

标签: c++

我是C ++的新手。你能不能帮助我摆脱错误:

错误C2259:'MinHeap':无法实例化抽象类

IntelliSense:返回类型与返回类型“const int&”不相同或协变。被覆盖的虚函数函数

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()
        {
            ..
        }   
};

2 个答案:

答案 0 :(得分:6)

问题在于const T& Top()int Top()。后者与前者不同,因此不是覆盖。相反,它隐藏基类功能。您需要返回与基类版本完全相同的内容:const int& Top() const Push(),BTW存在同样的问题。

答案 1 :(得分:2)

尝试

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

    public:
        bool IsEmpty() const
        {
            ..
        }

        const int& Top() const
        {
           ..         
        }

        void Push(const int& item)
        {
            ...
        }

        void Pop()
        {
            ..
        }   
};

请注意,Top和Push

使用const int&代替int
相关问题