从文件中读取数据后丢失数据

时间:2012-06-04 22:36:46

标签: c++ linked-list

我有一个fallowing类的对象,一个类是一个数据结构,我在main组合使用。 ADT(抽象数据类型)是链表。我从文件中读取输入数据和创建和对象在打印后看起来很好。在push_back()之后,第3个int变量初始化为0.所以示例和代码:

ex.in:

1 7 31
2 2 2
3 3 3

现在我从每一行创建对象,在打印时看起来像他们想象的那样,但是在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(我在main中调用此函数后打印对象,因为它们假定为x var(来自RAngle类)在所有情况下都会更改为0.)

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);
    }

0 个答案:

没有答案
相关问题