C ++中的Looser Throw Specifier

时间:2014-03-27 20:36:06

标签: c++ c++11

这个错误是什么意思?我该如何解决?这是导致它的标题代码:

class BadJumbleException : public exception {
public:
    BadJumbleException (const string& msg); // Constructor, accepts a string as the message
    string& what();                         // Returns the message string
private:
    string message;                         // Stores the exception message
};

这是源代码:

BadJumbleException::BadJumbleException (const string& m) : message(m) {}
string& BadJumbleException::what() { return message; }

编辑:这是错误:

  

对'虚拟BadJumbleException ::〜BadJumbleException()

的更宽松的抛出说明符

2 个答案:

答案 0 :(得分:23)

在C ++ 03中,根据§18.6.1/ 5,std::exception有一个析构函数,声明这样就不会抛出任何异常(a compilation error will be caused instead)。

语言要求当你从这种类型派生时,你自己的析构函数必须具有相同的限制:

virtual BadJumbleException::~BadJumbleException() throw() {}
//                                                ^^^^^^^

这是因为覆盖函数可能没有更宽松的抛出规范


在C ++ 11中,std::exception::~exception在库代码中明确地将 标记为throw()(或noexcept},但所有析构函数都是{{1默认情况下。

that rule would include your destructor and allow your program to compile开始,这使我得出结论,你不是真正编译为C ++ 11。

答案 1 :(得分:0)

我的一个示例代码遇到了类似的异常。我试图在c ++ 14中实现这一点。这是实现:

{
              MinTrackingEventID: '14864641',
              MaxTrackingEventID: '14888303',
              theDate: '02-28-2019',
              Clicks: '36.000000000',
              Upload: '2.000000000',
              Enter: '0.000000000',
              UploadRate: '0.05555555555555555555',
              EnterRate: '0.00000000000000000000',
              MessagesSent: '0'
            },