1

Book 继承 LibraryItem

class Book : public LibraryItem {

这是我在父类中使用构造函数的尝试。

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++ 给了我:

LibraryItem.cpp: 在构造函数'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' 未在此范围内声明 LibrayItem(title, callNumber, publisher, location, 'B', year);

于是我搜了一下,发现Implicitly call parent constructors。我试过:

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++ 给了我:

LibraryItem.cpp: 在构造函数'Book::Book(std::string, std::string, std::string, std::string, int, std::string, std::string, std::string, std ::string)': LibraryItem.cpp:71:193: 错误: 类 'Book' 没有任何名为 'LibrayItem' 的字段 Book::Book(std::string title, std::string callNumber, std::string出版商,std::string 位置,int 年,std::string 作者,std::string ISBN,std::string 主题,std::string 版本):LibrayItem(标题,callNumber,出版商,位置,'B',年) {

我不知所措,我检查了 Book 的头文件,它确实公开继承了 LibraryItem,所以我不确定问题出在哪里。

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

2 回答 2

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

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

工作正常,但您的代码中有一个错字很可能导致此问题:

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

应该是

: LibraryItem(title, callNumber, publisher, location, 'B', year) {
于 2013-09-20T06:24:29.380 回答
0

我认为在声明中你说它是 Libra* r *yItem 是基类,但在这里

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) {
    [...]
}

您正在使用缺少r的 LibrayItem 。这可能是问题所在。

于 2013-09-20T06:27:59.210 回答