重写模板基类中的纯虚拟函数

Overriding Pure Virtual Functions from a Template Base Class

本文关键字:虚拟 函数 基类 重写      更新时间:2023-10-16

所以我试图制作一个基于数组的包,它的行为就像一个堆栈,它添加到顶部,但也可以检查和删除不在顶部的元素。

在我的作业中,我的教授给了我这个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>中各自的纯虚拟函数,因为它们的签名本质上是不兼容的。当您不匹配constness时,也会发生这种情况。签名为void foo( int )的函数void foo( int ) const不兼容。前者在任何情况下都不会覆盖/实现后者。