从模板基类重写纯虚函数

时间:2017-02-10 01:06:47

标签: c++ inheritance pure-virtual

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

对于我的任务,我的教授用纯虚函数给了我这个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错误告诉我每个纯虚函数都没有覆盖。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

这是问题所在:

template <typename E>
bool addItem(const E& item)
{
    if (top < maxSize)
    {
        listArray[top] = item;
        top++;
        return true;
    }
    else
        return false;

}

你知道,抽象类D的任何子类C本身都被认为是抽象的,除非D绝对实现C的所有纯虚方法。 Bag<E>的{​​{1}}是一个带有签名addItem()的纯虚函数。另一方面,bool Bag<E>::addItem( const E& )的{​​{1}}是一个函数模板,它采用名为ABag<E>的类型模板参数,该参数会影响另一个addItem()E。实例化后,它将生成签名E

的功能

这里重要的一点是函数模板与函数不同。您可以将函数模板实例化到函数中,但这两个概念本身是不兼容的。 ABag<E>中的函数模板未在bool ABag<E1>::addItem<E2>( const E2& )中实现相应的纯虚函数,因为它们的签名本质上是不兼容的。如果您不匹配ABag<E>,也会发生这种情况。签名为Bag<E>的函数 const不兼容。在任何情况下,前者都不会覆盖/实施后者。