c++异常错误
C++ Exceptions error
我已经定义了我自己的异常,我在编译时得到错误:
/home/me/my_proj/CMyExceptions.hpp:38:63: error: no matching function for call to ‘MyException1::MyException1()’
MyException2(const std::string& msg) : m_msg(msg) {}
^
/home/me/my_proj/CMyExceptions.hpp:38:63: note: candidates are:
/home/me/my_proj/CMyExceptions.hpp:23:3: note: MyException1::MyException1(const string&)
MyException1(const std::string& msg) : m_msg(msg) {}
^
/home/me/my_proj/CMyExceptions.hpp:23:3: note: candidate expects 1 argument, 0 provided
/home/me/my_proj/CMyExceptions.hpp:17:7: note: MyException1::MyException1(const MyException1&)
class MyException1 : public std::exception
^
我的例外是:
class MyException1 : public std::exception
{
private:
std::string m_msg;
public:
MyException1(const std::string& msg) : m_msg(msg) {}
virtual ~MyException1() throw() {};
virtual const char* what() const throw()
{
return m_msg.c_str();
}
};
class MyException2 : public MyException1
{
private:
std::string m_msg;
public:
MyException2(const std::string& msg) : m_msg(msg) {}
virtual ~MyException2() throw() {};
virtual const char* what() const throw()
{
return m_msg.c_str();
}
};
我做错了什么?我应该在初始化之前调用父构造函数(MyException1
)吗?
您在MyException1中有m_msg并将其再次放在MyException2中。这意味着MyException2实际上包含两个m_msg变量。这是你想要的吗?
错误消息是因为MyException1没有默认构造函数,所以MyException2必须在初始化列表中调用它的构造函数。我建议
class MyException2 : public MyException1
{
public:
MyException2(const std::string& msg) : MyException1(msg) {}
};
相关文章:
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- 发生此错误:在0x0F2BFB7C(基于.dll)时引发异常 尝试更改大小是问题吗?
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 引发异常:读取访问冲突.**此**0x40错误
- 从 C# 调用 C++ DLib 会导致错误的分配异常
- 错误 : 异常 : 引发未处理的异常: 读取访问冲突.0xDDDDDDDD临时温度
- Windows错误:异常:使用从C++到Python的ctypes创建DLL时出现访问冲突或Windows错误193
- C++执行期间的类成员函数错误/异常处理
- 无法捕获C++长度错误异常
- 内存读/写错误异常
- 字符串转换错误:异常还是错误代码
- 类中的错误/异常捕获
- 在库中引发逻辑错误异常或中止
- POCO C++SAX解析器:如果xml文档编码是ANSI,那么下一条语句不是读取并抛出编码错误异常
- 捕获 Segfault 或任何其他错误/异常/信号C++例如在 Java 中捕获异常