-1

I have got a code snippet. When I am trying to compile it, I am getting an error:

       no matching function for call to ‘Journal::Journal()’
       dBestSeller, dCheckedOut, dCheckOutDate, dCustomer) {
                                                         ^

        Article::Article(int dId, QString dTitle, QList<QString> *dAuthors,
             QList<QString> *dkeywords, QString dPublisher, int dPrice,
             Journal dJournal,bool dReference, bool dBestSeller,
             bool dCheckedOut, QDate *dCheckOutDate, Patron *dCustomer)
        : Document(dId, dTitle, dAuthors, dkeywords, dPublisher, dPrice,
        dReference, dBestSeller, dCheckedOut, dCheckOutDate, dCustomer) 
        { 
           journal = dJournal;
        }

I suppose that somewhere there is an implicit constructor call. Where can it be?

Radical Ed
  • 178
  • 2
  • 13
  • 6
    Please show us a [mcve]. – iBug Feb 23 '18 at 11:39
  • 1
    Article has a a member "journal", and it doesn't have a default contructor? – peterchen Feb 23 '18 at 11:41
  • 2
    "_I suppose that somewhere there is an implicit constructor call. Where can it be?_" When execution enters the constructor body, **all** member variables must be constructed. Hence, if you have a member variable, that doesn't have default constructor, and don't initialize it before execution enters the constructor body, how should the compiler initialize it? – Algirdas Preidžius Feb 23 '18 at 11:45
  • I don't consider this to be a duplicate of the linked question. The OP is not asking about whether it is preferable to use initialization lists. The answers to the linked question provide information that helps answers this question, but the questions are different. – RHertel Feb 23 '18 at 13:00

1 Answers1

4

I suppose Journal doesn't have default constructor, then you need to initialize journal in member initializer list instead of assigning it in the constructor's body.

Member initializer list is the place where non-default initialization of these objects can be specified. For members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

i.e.

Article::Article(int dId, QString dTitle, QList<QString> *dAuthors,
     QList<QString> *dkeywords, QString dPublisher, int dPrice,
     Journal dJournal,bool dReference, bool dBestSeller,
     bool dCheckedOut, QDate *dCheckOutDate, Patron *dCustomer)
: Document(dId, dTitle, dAuthors, dkeywords, dPublisher, dPrice,
    dReference, dBestSeller, dCheckedOut, dCheckOutDate, dCustomer), 
  journal (dJournal)
{}

Otherwise, journal will be tried to be default initialized at first, then assigned in the constructor's body. Note that initialization and assignment are different things; before assignment inside the constructor's body, journal must be initialized.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405