Reputation: 155
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
Upvotes: 0
Views: 937
Reputation: 562
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
Upvotes: 2
Reputation: 1525
Library lib();
is wrong. This statement declares a function lib
which returns Library
and takes no parameter
This should have been Library lib;
Upvotes: 2