I am trying to use an ifstream object as an argument to a class constructor so that I can instantiate an array of objects. Currently I have the following in my header:
class Animal
{
private:
string name;
int portions;
int rarity;
int habitat;
int climate;
public:
Animal(ifstream &animalInput)
{
getline (animalInput, name, '\n');
animalInput >> portions;
animalInput >> rarity;
animalInput >> habitat;
animalInput >> climate;
animalInput.ignore(numeric_limits<streamsize>::max(), '\n');
}
};
And in my main.cpp:
const int numberOfAnimals = 12;
ifstream animalInput;
animalInput.open ("animals.txt");
if (animalInput.fail())
{
cout << "Opening file animals.txt failed";
exit(1);
}
Animal animals[numberOfAnimals](animalInput);
I am getting the error that no suitable conversion function from "std::ifstream" to "animals[12]" exists. My code then fails to compile because I do not have a default constructor. Am I passing ifstream incorrectly or is what I am trying to do impossible?