In the code below, I am trying to use a for loop to initialise the member Name of five objects of the class book using names taken from the string array Array (numbers in this case for testing purposes).
#include <iostream>
#include <string>
using namespace std;
class book
{
private:
string Name;
public:
book();
book(string&);
};
book :: book()
{}
book :: book(string& temp)
{
Name = temp;
}
int main()
{
string Array[] = {"1", "2", "3", "4", "5"};
book *BookList = new book[5];
for (int count = 0; count < 5; count ++)
{
BookList[count] = new book(Array[count]);
}
return 0;
}
However, whenever I try to compile the code, I get the following error:
main.cpp: In function ‘int main()’:
main.cpp:28: error: no match for ‘operator=’ in ‘*(BookList + ((unsigned int)(((unsigned int)count) * 4u))) = (operator new(4u), (<statement>, ((book*)<anonymous>)))’
main.cpp:6: note: candidates are: book& book::operator=(const book&)
My intention is to create an array of objects using private member values that will only be known once the loop collects the relevant data. I'm following the advice offered in answer #2 to a question I asked here previously.
BookListarray and trying to initialize it withbookobjects.Name.