我在寫東西來說明命令模式。我已經認識到,對於這個計算器實現,所有二進制操作(加法,減法等)只對最上面的兩個堆棧項執行操作,所以我試圖將該邏輯拉入另一個基類(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
}