【发布时间】:2025-11-25 15:05:01
【问题描述】:
我正在尝试动态分配对象,然后将这些指向对象的指针添加到向量中。但是我得到了错误: “请求从‘库 (*)()’转换为非标量类型‘库’”。我该如何纠正这个问题?
我已包含以下文件
//物品类
class Item
private:
std::string idCode;
std::string title;
public:
Item(std::string idc, std::string t)
{
this->idCode=idc;
this->title=t;
};
//Book类继承自Item类
class Book: public Item //class inherits from parent Item
{
private:
std::string author;
public:
//constructor
Book(std::string idcIn,std::string tIn,std::string authorIn)
:Item(idcIn, tIn)
{ author=authorIn;}
};
//保存指向指针向量的库类
class Library
private:
std::vector<Item*>holdings;
public:
void addLibraryItem(Item* item)
{
holdings.push_back(item);
}
这里是主文件
void addItem(Library); //prototype for adding Item function
int main()
{
Library lib(); //create Library object
addItem(lib); //ERROR POINTS TO HERE
return 0;
}
void addItem(Library lib)
{
Item *ptr=new Book("bookID", "Title", "Author")
lib.Library::addLibraryItem(ptr);
}
感谢您的宝贵时间
【问题讨论】:
标签: c++ class pointers inheritance vector