Я пишу что-то, чтобы проиллюстрировать шаблон команды. Я понял, что для этой реализации калькулятора все двоичные операции (сложение, вычитание и т. Д.) Просто «выполняют» операцию над двумя верхними элементами стека, поэтому я попытался перенести эту логику в другой базовый класс (BinaryCommand). ).
Я не понимаю, почему я получаю сообщение об ошибке (показано в комментарии к основной функции ниже). Любая помощь с благодарностью!
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
}