1

您好,我已经重载了插入和提取运算符。当我运行我的程序时,尽管提取正在将值放入类中,但插入似乎没有输出值。似乎实例的插入视图中没有任何值。

主要的

/ Input Poly
cout << "Input p1: " << endl;
Polynomial P1;
cin >> P1;

// Output Poly
cout << "p1(x) = " << P1 << '\n' << endl;

类功能

//Insertion
ostream& operator<<(ostream& os, Polynomial Poly){

for (int i=0; i < Poly.polyNum; i++) {
    os << Poly.poly[i] << " x^" << i;

    if(i != Poly.polyNum - 1){
        os << " + ";
    }
}

return os;
}

//Extraction
istream& operator>>(istream& is, Polynomial Poly){

int numP = 0;
int * tempP;

is >> numP;

tempP = new int [numP+1];

for (int i=0; i < numP; i++) {
    is >> tempP[i];
}

Poly.polyNum = numP;

Poly.poly = new int[Poly.polyNum +1];

for (int i=0; i < Poly.polyNum; i++) {
    Poly.poly[i] = tempP[i];
}

return is;
}
4

1 回答 1

4
istream& operator>>(istream& is, Polynomial Poly)

应该

istream& operator>>(istream& is, Polynomial& Poly)

您现在所做的只是更改对象副本的成员:

Polynomial P1;
cin >> P1;

P1在此之后不修改。

于 2012-09-27T14:19:41.620 回答