2012-10-19 35 views
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; 
} 

我重載輸入操作者輸入的對象。我面臨的問題是,當我輸入兩個對象後,第一個對象輸入的數據成員發生變化。重載操作者在>> C++

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

回答

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

+1但爲什麼要使用指針呢? 'std :: vector '應該足夠了。 – Praetorian

+0

@Praetorian:是的,你是對的。我沒有太注意這種類型的實際做法,我不得不承認...... –

1

變化poly p1, p2;poly p1(3), p2(3);

p.no_term3值,不過,看你的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的數組的指針指向您示例中的相同位置。填寫無效內存後,看到相同的結果。