2012-03-19 12 views
0

私はコマンドパターンを説明する何かを書いています。私は、この計算機の実装では、すべてのバイナリ演算(加算、減算など)が上位2つのスタック項目に対して演算を単に実行することを認識しているので、そのロジックを別の基本クラス(BinaryCommand )。なぜコンパイラはこのクラスが抽象クラス(C++)だと思いますか?

なぜ私はエラーが表示されているのか混乱しています(下のメイン関数のコメントとして表示されます)。どんな助けでも大歓迎です!

class ExpressionCommand 
{ 
public: 
    ExpressionCommand(void); 
    ~ExpressionCommand(void); 

    virtual bool Execute (Stack<int> & stack) = 0; 
}; 


class BinaryCommand : ExpressionCommand 
{ 
public: 
    BinaryCommand(void); 
    ~BinaryCommand(void); 

    virtual int Evaluate (int n1, int n2) const = 0; 

    bool Execute (Stack<int> & stack); 
}; 
bool BinaryCommand::Execute (Stack <int> & s) 
{ 
    int n1 = s.pop(); 
    int n2 = s.pop(); 
    int result = this->Evaluate (n1, n2); 
    s.push (result); 
    return true; 
} 

class AdditionCommand : public BinaryCommand 
{ 
public: 
    AdditionCommand(void); 
    ~AdditionCommand(void); 

    int Evaluate (int n1, int n2); 
}; 
int AdditionCommand::Evaluate (int n1, int n2) 
{ 
    return n1 + n2; 
} 


int main() 
{ 
    AdditionCommand * command = new AdditionCommand(); // 'AdditionCommand' : cannot instantiate abstract class 
} 

答えて

0

Eekは、派生クラスに 'const'を追加して申し訳ありません。

0

BinaryCommandは、抽象的なものです。virtual int Evaluate (int n1, int n2) const = 0;は純粋であると宣言されています。

AdditionCommandは、virtual int Evaluate (int n1, int n2) const = 0;を上書きしないため、クラスには純粋な仮想メンバーの定義がありません。したがって、抽象です。

int AdditionCommand::Evaluate (int n1, int n2);は、virtual int Evaluate (int n1, int n2) const = 0;を上書きしませんが、非表示にします。

関連する問題