C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
Weird error on 'exception specification of overriding function is more lax than base version' in C++
做一个项目,我正在尝试创建一个包含一堆类的 HandleError 标头。在我的类BadNumber
中,我有一个公共方法/函数,它接受字符串type
和num
。但是,当我尝试测试它时,我收到一个错误,要求exception specification of overriding function is more lax than base version
因为我是从public std::exception
继承的。我在谷歌上搜索了如何解决这个问题,它建议我在what()
中对此异常调用进行 noexcept 覆盖调用(链接在这里(。该示例几乎与我的相同,但具有相同的错误消息。
代码(使用 GCC/Clang c++11 编译(:
#include <exception>
#include <string>
class BadNumber : public std::exception
{
private:
std::string _msg;
public:
BadNumber(std::string type, std::string num) : _msg(num + "is invalid type for " + type) {}
const char *what() const noexcept override
{
return (_msg.c_str());
}
};
错误信息:
src/../inc/HandleError.hpp:23:15: error: exception specification of overriding function is more lax than base version
const char *what() const noexcept override
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/exception:102:25: note: overridden virtual function is here
virtual const char* what() const _NOEXCEPT;
我按照要求做了一些研究,但仍然没有运气,仍然做得更多。非常感谢您的帮助,获得一些反馈和建设性总是有帮助的。感谢您的时间和耐心:)
所以我意识到我必须将每个类作为虚拟析构函数,否则,我会使用我不想这样做的基本析构函数。这是我的解决方案:
class BadNumber : public std::exception
{
private:
std::string _msg;
public:
virtual ~BadNumber() throw() {return ;}
BadNumber(std::string type, std::string num) : _msg(num + "is invalid type for " + type) {}
virtual const char *what() const throw()
{
return (_msg.c_str());
}
};
相关文章:
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
- 我将 Makefile 更改为 C++11 版本,但仍然收到列表初始值设定项错误
- 链接库错误 c/缺少 dll/错误版本?
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 解决与构建"Debug x64"版本的暗网相关的错误
- 成功完成TLS握手后,服务器关闭时出现错误的SSL例程:SSL3_GET_RECORD:错误的版本号
- Qt 应用程序中与Qt版本相关的运行时错误
- C++发布版本中的未来错误值
- clang --版本权限被拒绝错误
- 错误:不支持 GLSL 3.30.支持的版本包括:1.10、1.20、1.30、1.00 ES、3.00 ES、3.10
- Qt Debugger在Mac上使用错误的python版本
- VS2015中内置的程序的发布版本与Qt运行然后关闭没有错误?
- 片段着色器中的"错误:在 GLSL 1.30 及更高版本中禁止使用非常量表达式索引的采样器数组"
- wxWidgets 运行时错误(不匹配版本)
- 协议缓冲区错误版本
- 与NVIDIA链接的错误GCC版本
- 如何处理加载错误的共享库版本的情况
- 在 CentOS 7 上通过 devtoolset-4 安装的 Eclipse CDT 使用了错误版本的 gcc/g++
- 由于mcordbi.dll的错误版本,托管调试不再起作用