我在使以下代碼工作時遇到了一些問題。我不能寫拋出異常,所以我不知道我還能做什麼。在類名嘗試拋出異常處理
case 't': // Top of Stack
try
{
cout << "Top() -- " << sPtr->Top() << endl; //this should run if the stack is not empty.
}
catch (StackEmpty)
{
cout << "Top() -- Failed Empty Stack" << endl; // this should run if the stack is empty.
}
break;
的特徵碼點頂部()函數棧這裏是這個函數的代碼:
int Stack::Top() const // Returns value of top integer on stack WITHOUT modifying the stack
{ cout << "*Top Start" << endl;
if(!IsEmpty())
return topPtr->data;
cout << "*Top End" << endl;
}
如果我刪除if語句,它會導致分段錯誤問題。
當學習代碼C++,檢查每個'if'沒有'else'並_really_肯定這是你想要的。 –