library first defined here

Oct 27, 2018 at 3:39pm
Hello. In my library program, I have 3 overloaded book constructors. One is a copy constructor, and the other 2 are normal contructors, which are overloaded. When I try to run my program, it won't let me overload them. Please help!

Library.main.cpp file:

#include <iostream>
#include "library.h"

using namespace std;


int main()
{
library l(2000);

const book book1("Diary of a wimpy kid", "Jeff Kinney", 2000);
const book book2("Percy Jackson and the sea of monsters", "Rick Riordan", 2003);
const book book3("Harry Potter", "JK Rowling", 1997);
const book book4 = book3;
const book book5 = book3;


l.addBook(book1);
l.addBook(book2);
l.addBook(book3);
l.addBook(book4);
l.addBook(book5);

l.showBooks();


return 0;
}


library.h file:
#ifndef LIBRARY_H
#define LIBRARY_H
#include <string>


class book {
private:
std::string title;
std::string author;
int publicationYear;
int ID;
public:
static int numberOfBooksAlreadyCreated;
std::string getTitle() const;
std::string getAuthor() const;
int getPublicationYear() const;
int getID() {return ID;}

book();
book(std::string, std::string, int);
book(const book &);
~book();
};

class library {
private:
book *books;
int maxAmountOfBooks;
int currentAmountOfBooks;
public:
// returning
int returnCurrentAmountOfBooks() {return currentAmountOfBooks;}
// Constructors and destructors
library(int);
~library();
// Methods
void addBook(book);
book getBook(int);
void showBooks();
};

#endif


library.cpp file:

#include "library.h"
#include <iostream>

using namespace std;

int book::numberOfBooksAlreadyCreated = 1;

book::book(const book &Book)
{
this->ID = numberOfBooksAlreadyCreated++;
this->title = Book.getTitle();
this->author = Book.getAuthor();
this->publicationYear = Book.getPublicationYear();
}

book::book(string title, string author, int publicationYear)
{
this->title = title;
this->author = author;
this->publicationYear = publicationYear;
ID = numberOfBooksAlreadyCreated++;
}

book::book()
{
}

book::~book()
{
}

string book::getTitle() const
{
return title;
}

string book::getAuthor() const
{
return author;
}

int book::getPublicationYear() const
{
return publicationYear;
}

library::library(int maxAmountOfBooks)
{
if (maxAmountOfBooks < 0)
cout << "Your max book amount cannot be less than 0!" << endl;

else {
currentAmountOfBooks = 0;
this->maxAmountOfBooks = maxAmountOfBooks;
books = new book[maxAmountOfBooks];
}
}

library::~library()
{
delete []books;
}

void library::addBook(book Book)
{


if (currentAmountOfBooks > maxAmountOfBooks)
cout << "You have succeeded the max amount of books! You cannot have more!" << endl;
else
books[currentAmountOfBooks] = Book;
currentAmountOfBooks++;
}

book library::getBook(int Book)
{
if (Book > maxAmountOfBooks || Book < 1)
return books[0];

else
return books[Book];
}

void library::showBooks()
{
for (int i = 0; i < currentAmountOfBooks; i++)
{
cout << "Book " << i + 1 << ": " << getBook(i).getTitle() << endl;
cout << "ID: " << books[i].getID() << endl;
}
}
Oct 27, 2018 at 4:29pm
Full error message and the line numbers to go with the code, please.
Oct 27, 2018 at 4:54pm
Also, build commands used (for compile and link)
Oct 27, 2018 at 4:59pm
It's able to compile now, for some reason.
Topic archived. No new replies allowed.