I have a program that manages a library. I have 3 classes.
1.- Class Biblioteca (Library) that manages with a dynamic array the books (array is named m_biblio), each position is one book (title...)
2.- Class Llibre (Book) that manages every book, that contains a dynamic array (array is named m_llibre) with the number of copies of every book.
3.- Class Exemplar (copie) with getters and setters for the atributte.
I have a function in the class Library that reads whats is in the file, and sets array size:
ifstream fitxer;
string titol, autor;
int maxllibres = 0;
int nExemplars=0;
int i = 0;
fitxer.open(nomFitxer);
if (fitxer.is_open())
{
fitxer >> maxllibres; //first line of txt reads an integer
setBiblioteca(maxllibres);
while (!fitxer.eof())
{
fitxer >> titol >> autor >> nExemplars;
m_biblio[i].setTitol(titol);
m_biblio[i].setAutor(autor);
m_biblio[i].setNExemplars(nExemplars);
i++;
}
}
fitxer.close();
this function calls setBiblioteca(maxllibres); and m_biblio[i].setNExemplars(nExemplars); to set the size of the arrays.
The function of setBiblioteca is like this:
{
m_maxllibres = Maxllibres;
m_biblio = new Llibre[m_maxllibres];
}
It's the same with setNExemplars.
With this situation, the program fails to set the size of the array. VS output is this std::bad_array_new_length. And I don't know what's the problem. If you could help I'll be very happy to solve this problem.
Thaaanks!