[英]runtime_error expecting a “;”?
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. 我正在尝试使用继承来创建一个从
runtime_error
派生的类,但是即使这是练习中使用的确切代码,也仍然是本书中的示例,但我一直遇到错误。 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.
在runtime_error(“试图除以零”)行之前。
调用基类构造函数的语法应为:
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" )
{
}
class DivideZeroEx : public runtime_error
{
public:
DivideZeroEx() : runtime_error( " 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. 在这种情况下,它并不会节省太多,但是如果您要在整个类中按名称使用
runtime_error
,那么将来换掉基数会更容易。
class DivideZeroEx : public runtime_error
{
typedef runtime_error base;
public:
DivideZeroEx() : base( " attempt to divide by zero" )
{
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.