简体   繁体   中英

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. 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.

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

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.

class DivideZeroEx : public runtime_error
{ 
typedef runtime_error base;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM