0

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

例如:

1 7 31
2 2 2
3 3 3

现在我从每一行创建对象,它们在打印时看起来像他们想象的那样,但是在 push_back() 之后:

1 7 0
2 2 0
3 3 0

类.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);
    }
4

0 回答 0