gpt4 book ai didi

c++ - 尝试在 C++ 中实现异常时出错

转载 作者:行者123 更新时间:2023-11-28 02:19:31 25 4
gpt4 key购买 nike

error: declaration of 'virtual const char* numberOutOfBounds::what() const' has a different exception specifier

我一直在寻找解决方案,但找不到我做错了什么。

我的头文件中的异常:

class FileException:public exception {
public:
virtual const char* what() const throw();
};

class numberOutOfBounds:public exception {
public:
virtual const char* what() const throw();
};

以及我的 cpp 文件中的异常(exception)情况:

const char* FileException::what() const {
return "Cannot open file";
}

const char* numberOutOfBounds::what() const {
return "illegal number entered";
}

有人可以让我知道我在这里做错了什么吗?我环顾四周,无法弄清楚为什么我会收到我收到的错误消息。

最佳答案

virtual const char* what() const throw();

此函数声明有一个 throw 声明,但定义中缺少它。

你需要:

const char* FileException::what() const throw() {
// ^^^^^^^ - added
return "Cannot open file";
}

仅供引用,已经有一个 std::out_of_range 异常类。

关于c++ - 尝试在 C++ 中实现异常时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33029384/

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