0

我正在构建一个双端队列,我只是在发生异常时向用户发送消息。所以,我在尝试从空列表中删除时使用了一个异常:

ArrayDeque 类:

 void ArrayDeque::deleteFront(){
  //Just check if list it's empty. If it is, it throw the exception.
  if(isEmpty())throw new logic_error("You can't delete from an empty 
  list");
  data.erase(data.begin()+front);

}

在 main 上调用函数:

try{
  deque->deleteFront();
}catch(logic_error e){
   cout<<e.what();
}

输出是:在抛出 'std::logic_error*' 的实例后调用终止

当我尝试从我的空数组中删除时。我包括了标准异常。

我如何才能返回消息:“您无法从空列表中删除”

4

1 回答 1

1

您正在使用throw new. 这与期望值对象的 catch 子句不匹配。

只需删除new. (并且可以选择通过 const-reference 捕获)。

于 2018-03-16T10:51:38.430 回答