将对象指针添加到位于 class 内的对象指针向量

Adding an object pointer to a vector of pointer-to-objects located inside a class

我正在尝试动态分配对象,然后将这些指向该对象的指针添加到向量中。但是我得到错误: "conversion from 'Library (*)()' to non-scalar type 'Library' requested"。我该如何纠正?

我已经包含了以下文件

//项目class

class Item
    private:
        std::string idCode;
        std::string title;
    public:
        Item(std::string idc, std::string t)
        {
            this->idCode=idc;
            this->title=t;
        };

//书籍Class继承自项目class

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 保存指向

的指针向量
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);
}         

感谢您的宝贵时间

Library lib(); 是错误的。该语句声明了一个函数 lib returns Library 并且不带参数

这应该是 Library lib;

Library lib() 更改为 Library lib

您应该将函数 addItem 的定义更改为

void addItem(Library& lib)
{
    Item *ptr=new Book("bookID", "Title", "Author")
    lib.addLibraryItem(ptr);
}

能够调用您的引用对象的方法addLibraryItem,而不是它的副本