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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -