c++ - Why does the compiler look for a default constructor for my exception class? -
i've defined small exception hierarchy library. inherits std::runtime_error, this:
class library_exception : public std::runtime_error { using std::runtime_error::runtime_error; }; class specific_exception : public library_exception { int guilty_object_id; specific_exception(int guilty_object_id_) : guilty_object_id(guilty_object_id_) {} };
the compiler says:
error: call implicitly-deleted default constructor of 'library_exception'
and points specific_exception constructor.
why trying call default constructor here?
library_exception
inherits std::runtime_error
. latter not have default constructor, means former isn't default constructable.
similarly, specific_exception
isn't default constructable because base class isn't. need default constructor base here because base implicitly initialized:
specific_exception(int guilty_object_id_) : guilty_object_id(guilty_object_id_) {}
to fix this, call appropriate base class constructor:
specific_exception(int guilty_object_id_) : library_exception("hello, world!"), guilty_object_id(guilty_object_id_) {}
Comments
Post a Comment