runtime_error期待“;”?

时间:2011-11-13 14:27:27

标签: c++ inheritance syntax

我正在尝试使用继承来创建一个派生自runtime_error的类,但我仍然会收到错误,即使这是练习中使用的确切代码,也是本书的一个例子。这是代码:

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

现在我收到一条错误消息,声明它预计有一个;在runtime_error之前(“尝试除以零”)行。

4 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:7)

您正在尝试在Member Initializer List中调用Base类构造函数,语法为:

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

答案 2 :(得分:1)

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

};

答案 3 :(得分:0)

在这种情况下,它不会节省太多,但如果您在整个课程中按名称使用runtime_error,那么将来更换基础会更容易。

class DivideZeroEx : public runtime_error
{ 
typedef runtime_error base;

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