gpt4 book ai didi

c++ - 在 C++ 中抛出超出范围的异常

转载 作者:IT老高 更新时间:2023-10-28 12:58:42 28 4
gpt4 key购买 nike

此代码有效;

int at(int index) {
if(index < 1 || index >= size)
throw 0;

return x[index];
}

但事实并非如此

int at(int index) {
if(index < 1 || index >= size)
throw std::out_of_range;

return x[index];
}

我收到错误“';'之前的预期主要表达式”。现在......这让我感到惊讶,因为我知道 std::out_of_range 存在并且我有

#include <stdexcept>

最佳答案

throw std::out_of_range; 替换为 throw std::out_of_range ("blah");。 IE。你需要创建一个对象,你不能抛出一个类型。

关于c++ - 在 C++ 中抛出超出范围的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709719/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com