2012-03-19 3 views
0

커맨드 패턴을 설명하기 위해 뭔가를 쓰고 있습니다. 나는이 계산기 구현을 위해 모든 2 진 연산 (더하기, 빼기 등)이 상위 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;이 순수하다고 선언 되었기 때문입니다.

AdditionCommandvirtual int Evaluate (int n1, int n2) const = 0;을 덮어 쓰지 않으므로 클래스에는 순수 가상 멤버에 대한 정의가 누락되어 있으므로 추상입니다.

int AdditionCommand::Evaluate (int n1, int n2);virtual int Evaluate (int n1, int n2) const = 0;을 무시하지만 숨 깁니다.

관련 문제