C ++不能实例化抽象类 [英] C++ cannot instantiate abstract class
问题描述
我是C ++的新手。你能帮我摆脱错误:
I am new to C++. Could you pls help me get rid of the errors:
错误C2259:'MinHeap':不能实例化抽象类
error C2259: 'MinHeap' : cannot instantiate abstract class
IntelliSense:返回类型不等于也不与返回类型const int&重写的虚拟函数函数
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()
{
..
}
};
推荐答案
问题是 const夯; Top()
与 int Top()
。后者不同于前者,因此不是覆盖。而是隐藏基类函数。你需要返回与基类版本完全相同: const int& Top()const
。
Push()
,BTW也有同样的问题。
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.
这篇关于C ++不能实例化抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!