重写模板基类中的纯虚拟函数
Overriding Pure Virtual Functions from a Template Base Class
所以我试图制作一个基于数组的包,它的行为就像一个堆栈,它添加到顶部,但也可以检查和删除不在顶部的元素。
在我的作业中,我的教授给了我这个bagADT类模板,它具有纯虚拟函数。
#ifndef BAGADT_H
#define BAGADT_H
#include <stdlib.h>
#include "book.h"
template <typename E>
class Bag {
public:
Bag() {} // base constructor
virtual ~Bag() {} // base destructor
// Insert a new item into the bag -- return false if fails and true if
// successful
virtual bool addItem(const E& item) = 0;
// Looks for 'item' in the bag and if found updates 'item' with the
// bag value and returns true. Otherwise 'item' is left unchanged
// and the method returns false.
virtual bool remove(E& item) = 0;
// Removes the top record from the bag, puts it in returnValue, and
// returns true if the bag is not empty. If the bag is empty the
// function returns false and returnValue remains unchanged.
virtual bool removeTop(E& returnValue) = 0;
// Finds the record using returnValue and if the record is found updates
// returnValue based on the contents of the bag and returns true. If the
// record is not found the function returns false. Works just like remove()
// except that the found record is not removed from the bag.
virtual bool find(E& returnValue) const = 0;
// Inspect the top of the bag. If the bag is empty return
// false and leave 'item' unchanged; otherwise, return true and update
// 'item' with the contents of the bag.
virtual bool inspectTop(E& item) const = 0;
// empties the bag
virtual void emptyBag() = 0;
// use the += operator to add an item to the bag
virtual bool operator+=(const E& addend) = 0;
// get the size of the bag
virtual int size() const = 0;
// get the capacity of the bag
virtual int bagCapacity() const = 0;
};
#endif /* BAGADT_H */
鉴于此,我创建了一个继承的ABag类。
#pragma once
#include "bagADT.h"
#ifndef ABAG_H
#define ABAG_H
template <typename E>
class ABag : public Bag<E>
{
public:
ABag(int size = 10)
{
maxSize = size;
top = 0;
listArray = new E[size];
}
virtual ~ABag()
{
delete[] listArray;
}
template <typename E>
bool addItem(const E& item)
{
if (top < maxSize)
{
listArray[top] = item;
top++;
return true;
}
else
return false;
}
// Looks for 'item' in the bag and if found updates 'item' with the
// bag value and returns true. Otherwise 'item' is left unchanged
// and the method returns false.
//template <typename E>
bool remove(E& item)
{
for (int i = 0; i <= top; i++)
{
if (listArray[i] == item)
{
for (int j = i + 1; j <= top; j++)
{
listArray[i] = listArray[j];
i++;
}
top--;
return true;
}
}
return false;
}
// Removes the top record from the bag, puts it in returnValue, and
// returns true if the bag is not empty. If the bag is empty the
// function returns false and returnValue remains unchanged.
//template <typename E>
bool removeTop(E& returnValue)
{
if (top > 0)
{
returnValue = listArray[top--];
}
}
// Finds the record using returnValue and if the record is found updates
// returnValue based on the contents of the bag and returns true. If the
// record is not found the function returns false. Works just like remove()
// except that the found record is not removed from the bag.
//template <typename E>
bool find(E& returnValue)
{
}
// Inspect the top of the bag. If the bag is empty return
// false and leave 'item' unchanged; otherwise, return true and update
// 'item' with the contents of the bag.
//template <typename E>
bool inspectTop(E& item)
{
if (top != 0)
{
item = listArray[top];
return true;
}
else
return false;
}
// empties the bag
//template <typename E>
void emptyBag()
{
top = 0;
}
// use the += operator to add an item to the bag
//template <typename E>
bool operator+=(const E& addEnd)
{
}
// get the size of the bag
//template <typename E>
int size()
{
return top;
}
// get the capacity of the bag
//template <typename E>
int bagCapacity()
{
return maxSize;
}
private:
int maxSize;
int top;
E *listArray;
};
/*template <typename E>
ABag<E>::ABag()
{
int size = 10
maxSize = size;
top = 0;
listArray = new E[size];
}
template <typename E>
ABag<E>::~ABag()
{
delete [] listArray;
}*/
#endif
这是我的source.cpp试图从类中实例化一个对象。
#include <iostream>
#include <string>
#include "ABag.h"
#include "BDictionary.h"
using namespace std;
int main(){
ABag<int> myBag;
cout << myBag.bagCapacity();
system("Pause");
return 0;
}
出于某种原因。我总是犯这个错误。
error C2259: 'ABag<int>' : cannot instantiate abstract class
我已经跋涉了所有的堆栈交换,文本书籍和一个又一个论坛,试图弄清楚为什么这不起作用。我明白错误是什么。我明白你不能从抽象类中生成对象。但我已经尝试了十几种不同的方法来覆盖基类中的纯虚拟函数,但我做不到。有一个IntelliSense错误告诉我,每个纯虚拟函数都没有覆盖器。
有人能帮我吗?
这就是问题所在:
template <typename E>
bool addItem(const E& item)
{
if (top < maxSize)
{
listArray[top] = item;
top++;
return true;
}
else
return false;
}
你看,抽象类C
的任何子类D
本身都被认为是抽象的,除非D
实现了C
的所有纯虚拟方法。Bag<E>
的addItem()
是一个具有签名bool Bag<E>::addItem( const E& )
的纯虚拟函数。另一方面,ABag<E>
的addItem()
是一个函数模板,它采用一个名为E
的类型模板参数,该参数对ABag<E>
中的另一个E
进行阴影处理。一旦实例化,它将产生签名为bool ABag<E1>::addItem<E2>( const E2& )
的函数。
这里重要的一点是,函数模板与函数不是一回事。您可以将函数模板实例化为函数,但这两个概念本身是不兼容的。ABag<E>
中的函数模板没有实现Bag<E>
中各自的纯虚拟函数,因为它们的签名本质上是不兼容的。当您不匹配const
ness时,也会发生这种情况。签名为void foo( int )
的函数与void foo( int ) const
不兼容。前者在任何情况下都不会覆盖/实现后者。
- C++无法定义虚拟函数 OUTER 类和头文件
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 有没有比在库中添加一个并非由所有派生类实现的新虚拟函数更好的设计实践
- 类型擦除的std::function与虚拟函数调用的开销
- 重写虚拟函数和继承
- 用纯虚拟函数兜圈子
- 为什么使用存储在虚拟方法表中的地址调用虚拟函数的函数会返回垃圾?
- 禁止子函数调用父级的抽象(或虚拟)函数
- 无法在子类中使用虚拟函数C++
- 无法在派生对象上运行虚拟函数
- 我可以调用从 main() 覆盖的虚拟函数吗?
- 在 C++ 中将函数获取和设置为虚拟函数
- 使用在堆栈上创建的对象调用虚拟函数
- 为什么在这种情况下不调用我的虚拟函数实现?
- 在C++中使虚拟函数私有化
- 模板继承类中的虚拟函数
- 为什么构造函数的虚拟函数调用有时有效,但其他调用却无效
- doxygenc++虚拟函数和实现
- 如何从派生类函数中调用虚拟函数