c++ - 如何在 C++ 中创建自定义的 logic_error 派生类?

标签 c++ c++11 stl

我想根据 stdexcept 的逻辑错误创建自己的错误。我尝试了以下并面临错误,我的猜测是,这不是正确的做法。我无法在谷歌或以前的堆栈溢出问题上找到答案,请原谅我的菜鸟。

// Header
class My_custom_exception : public logic_error
{
public:
    My_custom_exception(string message);
}
// Implementation
My_custom_exception::My_custom_exception(string message)
{
    logic_error(message);
}

我不知道如何执行。它给了我以下错误:

exception.cpp: In constructor ‘My_custom_exception::My_custom_exception(std::__cxx11::string)’:
exception.cpp:3:56: error: no matching function for call to ‘std::logic_error::logic_error()’
 My_custom_exception::My_custom_exception(string message)
                                                        ^
In file included from /usr/include/c++/5/bits/ios_base.h:44:0,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:1:
/usr/include/c++/5/stdexcept:128:5: note: candidate: std::logic_error::logic_error(const std::logic_error&)
     logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
     ^
/usr/include/c++/5/stdexcept:128:5: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/5/stdexcept:120:5: note: candidate: std::logic_error::logic_error(const string&)
     logic_error(const string& __arg);
     ^
/usr/include/c++/5/stdexcept:120:5: note:   candidate expects 1 argument, 0 provided
In file included from main.cpp:2:0:
exception.cpp:5:21: error: declaration of ‘std::logic_error message’ shadows a parameter
  logic_error(message);
                     ^
exception.cpp:5:21: error: no matching function for call to ‘std::logic_error::logic_error()’
In file included from /usr/include/c++/5/bits/ios_base.h:44:0,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:1:
/usr/include/c++/5/stdexcept:128:5: note: candidate: std::logic_error::logic_error(const std::logic_error&)
     logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
     ^
/usr/include/c++/5/stdexcept:128:5: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/5/stdexcept:120:5: note: candidate: std::logic_error::logic_error(const string&)
     logic_error(const string& __arg);
     ^
/usr/include/c++/5/stdexcept:120:5: note:   candidate expects 1 argument, 0 provided

最佳答案

您必须在类的构造函数的初始化列表中调用基类的构造函数。
像这样:

My_custom_exception::My_custom_exception(string message):
    logic_error(message)
{
}

关于c++ - 如何在 C++ 中创建自定义的 logic_error 派生类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44322444/

相关文章:

c++ - 在每次迭代中创建一个结构体

c++ - QT c++ 在使用信号和槽从另一个类调用类的方法时崩溃

c++ - 编译器认为 "A(A&)"暂时接受右值?

c++ - 当基类不是时,为什么派生类可以 move 构造?

android - 在 Android NDK r8b 下编译 STXXL

c++ - 在 C++ 中为循环编写迭代器的更好方法是什么

c++ - 内存中的 Ascii 到十进制,是否会有按位移位的答案?

c++ - boost::array<char, 2> 如何将 2 作为变量传递

c# - 编码字符 *

c++ - 替代 std::function 将函数作为参数传递(回调等)