usage of istream in class constructor c++ -
i have text file contains list of students , marks , looks this:
name_of_student 78 4; 98 5; 90 5; 63 3; ...
i have assignment create class read , store data. i've done far.
group.h class subject { public: subject(int mark0, int mark1); subject(); int get_m0() { return mark0; } int get_m1() { return mark1; } private: int mark0; int mark1; }; class student { public: student(string name); student(); vector<subject>my_marks; string get_name() { return name; } private: string name; }; class reading { public: reading(vector<student>, istream& ); istream& read_student(); private: vector<student>group; istream& is; }; text.cpp subject::subject(int m0, int m1) : mark0(m0), mark1(m1) {} subject::subject() : mark0(1), mark1(1) {} student::student(string n0) : name(n0) {} student::student() : name("null") {} reading::reading(vector<student>group0, istream& is0) : group(group0), is(is0) {} istream& reading::read_student() { string n; >> n; if (!is) return is; student st = student(n); (int = 0; < 4; ++i) { int m0, m1; >> m0 >> m1; char ch; >> ch; subject sub = subject(m0, m1); st.my_marks.push_back(sub); } group.push_back(st); return is; }
it compiles, refuses read anything.
int main() { ifstream ifs("text"); if(!ifs) error("can`t open input file"); vector<student> group; readding r(group, ifs); r.read_student(); cout << group.size(); }
and shows:
0
if has ideas i'd appreciate it.
in reading::read_student()
, filling member variable reading::group
. not filling function variable group
in main
.
use:
int main() { ifstream ifs("text"); vector<student> group; readding r(group, ifs); r.read_student(); cout << r.group.size(); // ^^^^^^^^ access member variable of r. }
if want function variable group
filled up, reading
needs store reference input group.
class reading { public: reading(vector<student> & , istream& ); // ^^^^^^ take reference input istream& read_student(); private: vector<student>& group; // ^^^^^^ store reference istream& is; };
Comments
Post a Comment