Adding book in library error

I'm trying to call a method called addBook but I keep getting some errors and I'm not sure what do these errors means.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void Bookshelf::addBook(int ID, string Title, string Author, vector<Book*> myBook){
	
	cout << "Enter an ID:";
	cin >> ID;
	cout << "Enter a title:";
	cin >> Title;
	cout << "Enter an author:";
	cin >> Author; 

	Book *mybook = new Book(ID, Title, Author);

	vector<Book*> listofBooks;
	listofBooks.push_back(mybook);
	
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){
	vector <Book*> myBook;

	Bookshelf * myBookshelf = new Bookshelf;

	myBookshelf->addBook(int ID, string Title, string Author, vector<Book*> myBook);  //error: int should be preceded by ')'
                        //function does not take 0 arguments
                        //syntax error:')' 



	return 0;



}
Well. Next time include these errors of yours because contrary to popular belief, we cant read minds.

You should read up on how functions work because you're using them wrong.

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
myBookshelf->addBook(int ID, string Title, string Author, vector<Book*> myBook); //error: int should be preceded by ')'

You need to fill in the values for the book you're adding when you call the function.

Also the vector that you pass to the function will not contain anything when the function is called as vector listofBooks is declared in the function and will be destroyed when the function ends, probably pass the vector into the function as a reference which means changing your function to something like:

1
2
3
void Bookshelf::addBook(int ID, string Title, string Author, vector<Book*> &myBook){
//function code here
}


and then use myBook instead of listofBooks, that way when the book is added to the list it will be added to the MyBook vector declared in main().



Topic archived. No new replies allowed.