我想在我的堆栈程序中抛出一个错误,只要我尝试在堆栈为空时一起添加数字。在我的堆栈类的顶层函数内部,如果堆栈中没有任何东西,我会引发异常。然后我继续在主程序中创建一个try和catch块来捕获错误并显示一条消息。但是,我收到下面列出的错误,我不知道如何解决它。C++ Try Catch Throw
错误:
terminate called after throwing an instance of 'char const*'
级顶尖():
const T& top() const throw (std::string){
if(m_next != NULL){
return m_data;
}
else{
throw("Nothing on the Stack");
}
};
INT主要():
int main(){
string op;
RobotCalc<int>* stack = new RobotCalc<int>;
int operand1;
int operand2;
cin >> op;
while(op != "@"){
if(op == "+"){
try{
operand1 = stack->top();
stack->pop();
operand2 = stack->top();
stack->pop();
stack->push(operand1 + operand2);
}
catch (string e){
cout << e;
}
}
还有更多的代码,但是这是哪里出了问题所在。类函数有2个成员变量:T类型的m_data(本例中为int)和一个指向下一个RobotClass(栈成员)的指针。这是一个堆栈的链接列表版本。