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:错误:'LibrayItem'不是 在此范围内声明LibrayItem(title,callNumber,publisher, 地点,'B',年份);
然后我搜索并找到了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:In 构造函数'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 publisher,std :: string location, int year,std :: string authors,std :: string ISBN,std :: string subject, std :: string edition):LibrayItem(title,callNumber,publisher, 地点,'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);
};
答案 0 :(得分: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) {
答案 1 :(得分:0)
我认为在宣言中你说它是天秤座* 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。这可能是问题所在。