2016-04-19 6 views
0

2つの汎用クラス(スタックとキュー)があり、スタックのほとんどの関数名(基本クラスを仮定)以下で使用して、私は、スタック内のこれらの機能のために別々のロジックを持っているでしょうし、キュージェネリックプログラミングで派生クラスの基本クラスメンバ関数を再宣言する必要があります

#ifndef STACK_H 
#define STACK_H 
template<class T> 
class myStack{ 
private: 
    T *s; 
    int top,s_size; 
public: 
    myStack(int k=40); 
    ~myStack(); 
    void push(T x); 
    T pop(); 
    int isEmpty(); 
    int isFull(); 
    T peek(); 
    void display(); 
}; 
#endif // STACK_H 

とはキュークラスのヘッダファイルである

#ifndef MYQUEUE_H 
#define MYQUEUE_H 
#include "stack.h" 


template <class T> 
class myQueue : public myStack<T> 
{ 
    private: 
     int f,r; 
    public: 
     void push(T x); 
     int isEmpty(); 
     int isFull(); 
     void display(); 
     myQueue(int k=40); 
     T del(); 
     T last(); 
     T first(); 
     virtual ~myQueue(); 

}; 

#endif // MYQUEUE_H 

その見かけ私は私のコードせずに、以下の機能を宣言し直していることコンパイルに失敗しました。本当にそれらを宣言する必要がありますか?私は再びそれを宣言しない場合は別々のロジックを持っているかの機能はすでに以下

 int isEmpty(); 
     int isFull(); 
     void display(); 

を継承していると私は、ヘッダーでそれらをせずに、派生クラスでこれらの機能を使用することができ、派生クラスは、エラーが発生しましたあなたは再定義子クラスのための方法にしたい場合は、派生クラス

||=== Build: Debug in Data_Structures (compiler: GNU GCC Compiler) ===| 
C:\Users\xprk569\Data_Structures\src\myQueue.cpp|13|error: no 'void myQueue<T>::display()' member function declared in class 'myQueue<T>'| 
C:\Users\xprk569\Data_Structures\src\myQueue.cpp|34|error: no 'void myQueue<T>::push(T)' member function declared in class 'myQueue<T>'| 
C:\Users\xprk569\Data_Structures\src\myQueue.cpp|41|error: no 'int myQueue<T>::isEmpty()' member function declared in class 'myQueue<T>'| 
C:\Users\xprk569\Data_Structures\src\myQueue.cpp|46|error: no 'int myQueue<T>::isFull()' member function declared in class 'myQueue<T>'| 
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 
+0

をあなたは 'myQueue'でそれを宣言しなくてもmyQueue'' 'ためisEmpty'を再定義している意味ですか? – Holt

+0

@holtはい、excatly – user2256825

答えて

1

に、あなたはそれを再宣言する必要があり、以下では動作しません:

struct A { 
    void foo() { } 
}; 

struct B: public A { }; 

// error: no 'void B::foo()' member function declared in class 'B' 
void B::foo() { } 

はまた、あなたがポリモーフィズムの使用(オーバーライド基本法)を作りたい場合、あなたはあなたの方法は、仮想にする必要があることに注意してください。

struct A { 
    virtual void foo() { } 
}; 

struct B: public A { 
    virtual void foo(); 
}; 

void B::foo() { } 

C++ 11は、そのあなたができる新しいoverrideキーワードを導入しましたあなたが本当に上書きされているかどうかを確認するために使用します。

struct A { 
    void foo() { } 
}; 

struct B: public A { 
    // error: 'void B::foo()' marked 'override', but does not override 
    void foo() override { } 
}; 
関連する問題