0

I'm trying to dynamically allocate objects, then add those pointers to the object to a vector. However I get the error: "conversion from 'Library (*)()' to non-scalar type 'Library' requested". How do I correct this?

I've included the files below

//Item class

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

//Book Class inheriting from Item 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;}
};

//Library class which holds the vector of pointers to

class Library
    private:
        std::vector<Item*>holdings;
    public:
        void addLibraryItem(Item* item)
        {
            holdings.push_back(item);
        }

And here is the main file

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

Thank you for your time

3 Answers 3

2

Library lib(); is wrong. This statement declares a function lib which returns Library and takes no parameter

This should have been Library lib;

Sign up to request clarification or add additional context in comments.

Comments

2

you should change definition of function addItem to

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

to be able to call method addLibraryItem of your referenced object, not copy of it

Comments

1

Change Library lib() to Library lib.

1 Comment

What does it mean when it will fall out of scope?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.