Call the parent constructor via the child's implementation

advertisements

Book inherits LibraryItem

class Book : public LibraryItem {

Here is my attempt at using the constructor in the parent class.

Book::Book(std::string title, std::string callNumber, std::string publisher, std::string location, int year, std::string authors, std::string ISBN, std::string subject, std::string edition) {
    LibrayItem(title, callNumber, publisher, location, 'B', year);
    this->authors = authors;
    this->ISBN = ISBN;
    this->subject = subject;
    this->edition = edition;
}

g++ gives me:

LibraryItem.cpp: In constructor ‘Book::Book(std::string, std::string, std::string, std::string, int, std::string, std::string, std::string, std::string)’: LibraryItem.cpp:72:62: error: ‘LibrayItem’ was not declared in this scope LibrayItem(title, callNumber, publisher, location, 'B', year);

So then I searched and found Implicitly call parent constructors. I tried:

Book::Book(std::string title, std::string callNumber, std::string publisher, std::string location, int year, std::string authors, std::string ISBN, std::string subject, std::string edition) : LibrayItem(title, callNumber, publisher, location, 'B', year) {
    [...]
}

g++ gives me:

LibraryItem.cpp: In constructor ‘Book::Book(std::string, std::string, std::string, std::string, int, std::string, std::string, std::string, std::string)’: LibraryItem.cpp:71:193: error: class ‘Book’ does not have any field named ‘LibrayItem’ Book::Book(std::string title, std::string callNumber, std::string publisher, std::string location, int year, std::string authors, std::string ISBN, std::string subject, std::string edition) : LibrayItem(title, callNumber, publisher, location, 'B', year) {

I'm at a loss, I checked my header file for Book and it does inherit LibraryItem publicly, so I'm not sure what the problem is.

class Book : public LibraryItem {
    private:
        [...]
    public:
        Book();
        Book(std::string, std::string, std::string, std::string, int, std::string, std::string, std::string, std::string);
};


class LibraryItem
{
    public:
        LibraryItem(int) {}
};

class Book
    : public LibraryItem
{
    public:
        Book(int)
            : LibraryItem(0)
        {}
};

Works fine, but you have a typo in your code that most likely causes this problem:

: LibrayItem(title, callNumber, publisher, location, 'B', year) {

which should be

: LibraryItem(title, callNumber, publisher, location, 'B', year) {