How to inherit from std :: runtime_error?

For example:

#include <stdexcept> class A { }; class err : public A, public std::runtime_error("") { }; int main() { err x; return 0; } 

With ("") after runtime_error I get:

 error: expected '{' before '(' token error: expected unqualified-id before string constant error: expected ')' before string constant 

else (without ("") ) I get

 In constructor 'err::err()': error: no matching function for call to 'std::runtime_error::runtime_error()' 

What will go wrong?

(You can test it here: http://www.compileonline.com/compile_cpp_online.php )

+8
c ++ inheritance runtime-error
source share
1 answer

This is the correct syntax:

 class err : public A, public std::runtime_error 

And not:

 class err : public A, public std::runtime_error("") 

As you do above. If you want to pass an empty string to the std::runtime_error , do this as follows:

 class err : public A, public std::runtime_error { public: err() : std::runtime_error("") { } // ^^^^^^^^^^^^^^^^^^^^^^^^ }; 

Here is a live example to show code compilation.

+13
source share

All Articles