2017-02-10 15 views
1

私はトップに追加する意味でスタックのように振る舞う配列ベースのバッグを作ろうとしていますが、それ以外の要素も検査して削除することができますトップ。テンプレートベースクラスからの純粋仮想関数のオーバーライド

私の任務のために、純粋な仮想機能を使って私の教授からこの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 

私は、これは動作しません理由を理解しようとフォーラムの後、すべてのスタック交換と教科書やフォーラムを越えtrekkedています。私はエラーが何であるか理解しています。私はあなたが抽象クラスからオブジェクトを作ることができないことがわかります。しかし、私は基本クラスから純粋仮想関数をオーバーライドしようとする十数の異なる方法を試しましたが、できません。純粋仮想関数のそれぞれにオーバーライダーがないことを示すIntelliSenseエラーがあります。

誰でもお手伝いできますか?

+1

最初に行うことは、 'class ABag'の' addItem'の上の 'template 'を失うことです。それはそこに属しておらず、クラステンプレートパラメータを陰にしています。あなたは少なくとも、それについての警告を受け取ったはずです。それを越えて、基本から派生への 'const'と非constメンバー状態の混合は確かにどちらも役に立たない。 Ex: 'find'はあなたのベースでは' const'として宣言されますが、あなたの派生では宣言されません。 – WhozCraig

+0

私はそれをコメントして、私はまだ同じエラーが発生します。私はそれについて警告を受けなかった。また、派生クラスの継承された関数でconstを使用しようとすると、メンバー以外の関数で修飾子を使用できないというエラーが表示されます –

+0

そのコメントの2番目の部分を読んでください。あなたはあなたの拠点に 'const'であり、あなたの派生物ではないメンバーの過多を持っています。定数を含めた*全体の*シグネチャに一致する必要があります。 – WhozCraig

答えて

0

これが問題である:

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

} 

Dは絶対にすべてのCの純粋仮想メソッドを実装していない限り、抽象クラスCの任意のサブクラスDは、抽象自体考えられています。 Bag<E>addItem()は、署名がbool Bag<E>::addItem(const E&)の純粋仮想関数です。一方ABag<E>addItem()は、Eというタイプのテンプレートパラメータをとり、EABag<E>にシャドーする関数テンプレートです。一旦インスタンス化されると、それは署名bool ABag<E1>::addItem<E2>(const E2&)の機能をもたらす。

ここで重要なのは、関数テンプレートが関数と同じではないということです。 関数テンプレートを関数にインスタンス化することはできますが、両方の概念自体は互換性がありません。 ABag<E>の関数テンプレートは、その署名が本質的に互換性がないため、Bag<E>にそれぞれの純粋仮想関数を実装していません。これは、constと一致しない場合にも発生します。 void foo(int)の関数は、void foo(int) constと互換性があり、ではなく、ではありません。前者は、いかなる状況においても、あとで、後でオーバーライド/実装することはありません。

+0

ありがとうございました!それは今まで働いた。それだけでなく、基本クラスには 'const'というキーワードがありませんでした。 私は十分に感謝することはできません –

関連する問題