ifstream 객체를 클래스 생성자에 대한 인수로 사용하여 객체 배열을 인스턴스화 할 수 있습니다.ifstream을 클래스 생성자에 인수로 전달
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');
}
};
그리고 내 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);
내가 오류를 얻고 그 "표준 : : ifstream"에서 "동물에서 더 적합한 변환 기능 없음 [12] "존재합니다. 기본 생성자가 없기 때문에 내 코드가 컴파일되지 않습니다. 내가 ifstream을 잘못 전달했거나 불가능하게하려고 시도한 것이 무엇입니까?
'main.cpp'에서와 같은 방식으로 객체 배열을 초기화 할 수 없습니다. – Pradhan