I'm writing a small class Student. My goal is to write the data of an object of the class to a txt file, then read all lines in the file and create the appropriate number of objects from the same file.
These are the private data members
class Student {
char* m_name = new char[1000];
int m_number;
double m_grade;
The getters:
char* getName () const {return m_name;}
int getNum () const {return m_number;}
double getGrade () const {return m_grade;}
Operator >>:
ostream& operator<< (ostream& ost, const Student& obj){
ost << obj.m_name << endl << obj.m_number << endl << obj.m_grade << endl;
}
(the info in the txt files for 2 objects looks like this)
John Doe
12345
5.67
Jane Doe
98765
66666
4.44
Operator >> (I really need suggestions how to overload it better. I don't like how I've done it. Also is it possible to overload it using "const Student& obj" as second argument?)
istream& operator>> (istream& ist, Student& obj){
char* temp = new char[255];
int tempNum = 0;
double tempGrade = 0;
ist.getline(temp, 255);
obj.setName(temp);
ist >> tempNum;
ist >> tempGrade;
obj.setNum(tempNum);
obj.setGrade(tempGrade);
}
And finally my load function: (It doesn't work, runtime (array boundaries) error)
vector<Student> load () {
ifstream ifs ("Data.txt");
while (ifs){
Student temp;
ifs >> temp;
cout << temp << endl;
load.push_back(temp);
}
I really would appreciate any suggestions on how to make the load function work using my ">>" operator and also how to overload ">>" better