vendredi 2 juin 2017

How to create custom derived class of logic_error in c++?

I'd like to create my own errors off the logic error from stdexcept. I tried the following and am facing errors, my guess is, its not the proper way of doing it. I was not able to find answers on google or previous stack overflow questions, pardon my noob-ness.

// 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);
}

I do not know how to do the implementation. It gives me following errors:

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

Aucun commentaire:

Enregistrer un commentaire