简体   繁体   English

如何从std :: runtime_error继承?

[英]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: runtime_error之后使用("")我得到:

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's going wrong? 出了什么问题?

(You can test it here: http://www.compileonline.com/compile_cpp_online.php ) (你可以在这里测试一下: http//www.compileonline.com/compile_cpp_online.php

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 are doing above. 正如你上面所做的那样。 If you want to pass an empty string to the constructor of std::runtime_error , do it this way: 如果要将空字符串传递给std::runtime_error的构造函数,请按以下方式执行:

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

Here is a live example to show the code compiling. 这是一个显示代码编译的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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