在C++中重载运算符 >>

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

+1 但为什么要使用指针呢?std::vector<term> 应该足够了。 - Praetorian
@Praetorian:是的,你说得对。我必须承认,我没有太关注这个类型实际上做了什么... - Dietmar Kühl

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

0

你的默认构造函数参数 d 的值为 0。然后你调用了 new term[0]。在你的示例中,初始化为长度为 0 的指针指向相同的位置。当你填充非有效内存并查看相同的结果时。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接