运行时错误需要“;”吗?

发布于 2024-12-15 09:12:06 字数 311 浏览 2 评论 0原文

我试图使用继承来创建一个从 runtime_error 派生的类,但我不断收到错误,即使这是练习中使用的确切代码以及本书中的示例。这是代码:

class DivideZeroEx : public runtime_error
{
    public:
        DivideZeroEx()
            runtime_error( " attempt to divide by zero" )
};

现在我收到一个错误,指出它需要一个 ;在runtime_error(“尝试除以零”)行之前。

I am trying to use inheritance to create a class derived from runtime_error, but I keep getting an error even though this is the exact code used in exercises and as an example from the book. Here is the code:

class DivideZeroEx : public runtime_error
{
    public:
        DivideZeroEx()
            runtime_error( " attempt to divide by zero" )
};

Now I get an error stating that it expects a ; before the runtime_error("attempt to divide by zero") line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就像说晚安 2024-12-22 09:12:06

调用基类构造函数的语法应该是:

DivideZeroEx() : runtime_error( " attempt to divide by zero" ) { }

The syntax for calling your base class constructor should be:

DivideZeroEx() : runtime_error( " attempt to divide by zero" ) { }
夏日落 2024-12-22 09:12:06

您正在尝试调用成员初始值设定项列表中的基类构造函数,语法为:

DivideZeroEx():runtime_error( " attempt to divide by zero" )
{
}

You are trying to call the Base class constructor in Member Initializer List, the syntax is:

DivideZeroEx():runtime_error( " attempt to divide by zero" )
{
}
九局 2024-12-22 09:12:06
class DivideZeroEx : public runtime_error
{ 
public: 
   DivideZeroEx() : runtime_error( " attempt to divide by zero" ) 
   {
   }

};
class DivideZeroEx : public runtime_error
{ 
public: 
   DivideZeroEx() : runtime_error( " attempt to divide by zero" ) 
   {
   }

};
云朵有点甜 2024-12-22 09:12:06

在这种情况下,它并没有节省太多,但是如果您要在整个类中按名称使用 runtime_error ,那么将来更换基类会更容易。

class DivideZeroEx : public runtime_error
{ 
typedef runtime_error base;

public: 
   DivideZeroEx() : base( " attempt to divide by zero" ) 
   {
   }
};

In this instance it doesn't save much, but if you were to use runtime_error by name throughout your class, this is much easier to swap out the base in the future.

class DivideZeroEx : public runtime_error
{ 
typedef runtime_error base;

public: 
   DivideZeroEx() : base( " attempt to divide by zero" ) 
   {
   }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文