# 收到我无法理解的错误[重复]

```超时：受监视的命令转储核心
sh：行1：29642分段错误```

The coefficient should not be stored if the value is zero, and that's why I have made a check in `getcoef()`.

``````#include <iostream>
#include <map>
using namespace std;

class Polynomial
{
friend Polynomial& operator+(Polynomial &p1, Polynomial &p2);
friend void display(Polynomial &p);
public:
map<int, double> poly;
public:
void getcoef()
{
cout<<"Enter degree of the polynomial";
int d;
cin>>d;
for(int i=0; i<=d; ++i)
{
double ai;
cin>>ai;
if(ai != 0)
poly[i] = ai;
}
}
};

void display(Polynomial &p)
{
for(map<int, double>::iterator x= p.poly.begin(); x!=p.poly.end(); ++x)
{
cout<<x->first<<" : "<<x->second<<endl;
}
}

Polynomial& operator+(Polynomial &p1,Polynomial &p2)
{
Polynomial p;
map<int, double>::iterator i= p1.poly.begin();
for(i; i!=p1.poly.end(); ++i )
{
p.poly[i->first] = i->second;
}

map<int, double>::iterator k = p2.poly.begin();
for(k; k!=p2.poly.end(); ++k)
{
map<int, double>::iterator j=p.poly.find(k->first);
if(j!=p.poly.end())
p.poly[j->first] += k->second;
else
p.poly[k->first] = k->second;
}
display(p);
return p;
}

int main()
{
Polynomial p1,p2;
p1.getcoef();
p2.getcoef();
Polynomial p3;
p3 = p1+p2;
display(p3);
}
``````