c++ - Losing data after reading them correct from file -
i have fallowing class of object class data structure use in main combined. adt(abstract data type) linked list. after read file input data , create , object @ print looks fine after print. after push_back() 3-rd int variable initializated 0. example , code:
ex.in:
1 7 31 2 2 2 3 3 3 now create objects each line, @ print suppose, after push_back():
1 7 0 2 2 0 3 3 0 class.h:
class rangle { private: int x, y, l, b; public: int solution,prec; rangle(){ x = y = solution = prec = b = l =0; } rangle(int i,int j,int k){ x = i; y = j; l = k; solution = 0; prec=0; b=0; } friend ostream& operator << (ostream& out, const rangle& ra){ out << ra.x << " " << ra.y << " " << ra.l <<endl; return out; } friend istream& operator >>( istream& is, rangle& ra){ >> ra.x; >> ra.y; >> ra.l; return ; } }; adt.h:
template <class t> class list { private: struct elem { t data; elem* next; }; elem* first; t pop_front(){ if (first!=null) { t aux = first->data; first = first->next; return aux; } t a; return a; } void push_back(t data){ elem *n = new elem; n->data = data; n->next = null; if (first == null) { first = n; return ; } elem *current; for(current=first;current->next != null;current=current->next); current->next = n; } main.cpp(after call function in main prints object suppose x var(from rangle class) changes 0 in cases.)
void readdata(list <rangle> &l){ rangle r; ifstream f_in; f_in.open("ex.in",ios::in); for(int i=0;i<10;++i){ f_in >> r; cout << r; l.push_back(r); }
Comments
Post a Comment