4
#include<iostream>
using namespace std;
class term
{
public:
    int exp;
    int coeff;  
};
class poly
{
public:
    term* term_ptr;
    int no_term;

    poly(int d);
    friend istream& operator>>(istream& in, poly& p);
    friend ostream& operator<<(ostream& out, const poly& p);
    friend poly operator+(const poly& p1, const poly& p2);
};
poly::poly(int d=0)
{
    no_term = d;
    term_ptr = new term[no_term];
}
istream& operator>>(istream& in, poly& p)
{
    in>>p.no_term;
    for(int i= 0; i<p.no_term; i++)
    {
        in>>(p.term_ptr+i)->coeff;
        in>>(p.term_ptr+i)->exp;
    }
    return in;
}

我重载了输入运算符来输入对象。我面临的问题是当我输入两个对象时,第一个对象输入的数据成员随之发生变化。

int main(void)
{
    poly p1, p2;
    cin>>p1;
    cin>>p2;
    cout<<p1;
    cout<<p2;
    return 0;   
}

如果输入是

 3
 1 1
 1 2
 1 3
 3
 1 1
 1 2
 1 3

我得到的输出是

1 1
1 2 
1 1
1 1
1 2
1 3

输出算子函数是

ostream& operator<<(ostream& out, const poly& p)
{
    out<<"coeff"<<" "<<"power"<<endl;
    for(int i = 0; i< p.no_term; i++)
        out<<(p.term_ptr+i)->coeff<<" "<<(p.term_ptr+i)->exp<<endl;
    return out;
}
4

3 回答 3

2

您最初分配一个包含零元素的数组。读取对象时,您会读取术语的数量,但不会重新分配术语数组。我个人建议使用合适的容器类型,例如,std::vector<term*>或者,实际上,std::vector<std::shared_ptr<term>>. 如果你坚持使用数组,你需要这样的东西:

std::istream& operator>>(std::istream& in, poly& p)
{
    if (in>>p.no_terms ) {
        std::unique_ptr<term[]> terms(new term[p.no_terms]);
        for(int i= 0; i<p.no_term; i++)
        {
            in >> terms[i].coeff;
            in >> terms[i].exp;
        }
        if (in) {
            delete[] p.term_ptr;
            p.term_ptr = terms.release();
        }
    }
    return in;
}
于 2012-10-19T06:22:01.347 回答
1

更改poly p1, p2;poly p1(3), p2(3);

p.no_term有一个值3,但是,看看你的poly构造函数:

poly::poly(int d=0)
{
    no_term = d;
    term_ptr = new term[no_term];
}

0您正在创建一个长度数组。此外,无需在代码中使用指针。这是一个使用示例std::vector<term>

#include<iostream>
#include <vector>
using namespace std;
class term
{
    public:
    int exp;
    int coeff;  
};
class poly
{
    public:
    std::vector<term> term_vec;
    int no_term;

            poly(int d);
            friend istream& operator>>(istream& in, poly& p);
            friend ostream& operator<<(ostream& out, const poly& p);
            friend poly operator+(const poly& p1, const poly& p2);
};
poly::poly(int d=0) : term_vec(d), no_term(d)
{
}
istream& operator>>(istream& in, poly& p)
{
    in>>p.no_term;
    p.term_vec.resize(p.no_term);
    for(int i= 0; i<p.no_term; i++)
    {
        in>> p.term_vec[i].coeff;
        in>> p.term_vec[i].exp;
    }
    return in;
}

  ostream& operator<<(ostream& out, const poly& p)
   {
    out<<"coeff"<<" "<<"power"<<endl;
    for(int i = 0; i< p.no_term; i++)
    out<<p.term_vec[i].coeff<<" "<<p.term_vec[i].exp<<endl;
    return out;
   }

   int main(void)
 {
    poly p1, p2;
    cin>>p1;
    cin>>p2;
    cout<<p1;
    cout<<p2;
    return 0;   
 }
于 2012-10-19T06:20:37.633 回答
0

您的默认构造函数参数d具有 value 0。然后你打电话new term[0]。在您的示例中,初始化为长度为 0 的数组的指针指向相同的位置。填充无效内存后,看到相同的结果。

于 2012-10-19T06:21:58.337 回答