Losing data after reading them correct from file
Posted
by
user1388172
on Stack Overflow
See other posts from Stack Overflow
or by user1388172
Published on 2012-06-04T22:36:46Z
Indexed on
2012/06/04
22:40 UTC
Read the original article
Hit count: 248
i have the fallowing class of object with a class a data structure which i use in main combined. The ADT(abstract data type) is a linked list. After i read from file the input data and create and object which at print looks just fine after a print. after i push_back()
the 3-rd int variable get initializated to 0. So example and code:
Example:
ex.in:
1 7 31
2 2 2
3 3 3
now i create objects from each line, which at print look as they suppose, but 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){
is >> ra.x;
is >> ra.y;
is >> ra.l;
return is ;
}
};
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 i call this function in main which prints object as they suppose to be the x var(from RAngle class) changes to 0 in all 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);
}
© Stack Overflow or respective owner