2014-09-27 77 views
0

我想重載一個類Poly的+運算符,以便在main.cpp中我可以一起添加到Poly中。我試圖做到這一點的方法是:創建新節點的問題

  1. 比較左側expnt與右側expnt
  2. 創建保利臨時與新發現的值的新節點(在解釋評論)
  3. 然後將新的Poly返回到main.cpp以存儲在pList []數組中。

出於某種原因,新的節點將被創建,但指針節點丟失這樣的main.cpp無法訪問溫度。我哪裏錯了?

// class Poly header file 
class Poly { 
private: 
// DATA MEMBERS 
struct Node 
{ 
    double coEf; 
    int expnt; 
    Node* next; 
}; 

Node* first; 
. 
. 
. 
} 

// class poly definition file 
// Private function 
Poly::Node* Poly::get_node(const double num1, const int num2, Node* link) 
{ 
    Node *temp; 

    temp = new Node; 
    temp -> coEf = num1; 
    temp -> expnt = num2; 
    temp -> next = link; 
    return temp; 
} 


Poly Poly::operator + (Poly source) 
{ 
    Node* p1 = first; 
    Node* p2 = source.first; 
    Node* last; 
    Poly temp; 
    Node* pt = temp.first; 

    // If the left side Poly is empty then just return the right side Poly. 
    if(first==NULL) 
    { 
     Poly(source); 
    } 
    // If the left side Poly's first exponent is equal to the right side Poly's 
    // exponent, then create a node for Poly temp with the following values: 
     // expnt = (because the expnt's are equal it doesn't matter which one) (int) 
     // coEf = both left side and right side's coEf's added together (double) 
     // next = NULL (because it's the end of temp's list) 
    if(p1 -> expnt == p2 -> expnt) 
    { 
     pt = get_node(((p1 -> coEf)+(p2 -> coEf)), p1 -> expnt, NULL); 
     p2 = p2 -> next; 
     p1 = p1 -> next; 
    } 
    if(p1 -> expnt > p2 -> expnt) 
    { 
     pt = get_node(p1 -> coEf, p1 -> expnt, NULL); 
     p1 = p1 -> next; 
    } 
    if(p1 -> expnt < p2 -> expnt) 
    { 
     pt = get_node(p2 -> coEf, p2 -> expnt, NULL); 
     p2 = p2 -> next; 
    } 
    else 
    { 
     pt = get_node(1, 2, NULL); 
    } 
return temp;  
} 

// main.cpp 
// function that calls the class member function operator +(Poly source) 
void addPoly() 
{ 
int add1; 
int add2;   
int add3; 
cout << "Enter the name of two polynomials to add them: " << endl; 
cin >> add1 >> add2; 
cout << "Enter a name for the new added polynomial: " << endl; 
cin >> add3; 
pList[add3] = pList[add1] + pList[add2]; 

} 
+0

我想你的意思是'Node *&pt = temp.first;',所以每個'pt'的修改都會修改'temp'。 – Jarod42 2014-09-27 16:21:34

回答

0

的問題:

的問題是,你不把任何東西temp

什麼也沒有發生,當你寫:

Poly temp;  // create an default initialized Poly 
Node* pt = temp.first; // this is a local variable. You just copy a pointer to a node 

... 

if(first==NULL) 
{ 
    Poly(source); // You create an object but don't store nor return anything!! 
} 

後來你存儲在PT,其中僅修改本地指針變量一個get_note()。指向的原始值不會更改,因此它對temp沒有影響!

將p1和p2分配給它們的下一個元素時,會遇到類似的問題:它只對局部變量有影響,並且不會影響poly。順便說一句,如果你嘗試添加多項式,請注意,你只能在這裏處理第一項,並且你沒有正確處理剩下的項。

方式來解決:

爲了您的第一回:

if(first==NULL) 
{ 
    return source; // You have return by value, so source will be copied 
} 

然後你return temp;之前,添加以下語句:

temp.first = pt; 

如果找你加入polynoms ,你應該考慮創建一個循環來處理不僅是第一個術語,還有剩下的術語。

+0

pt是temp內的一個指針。 – mosmic12 2014-09-27 16:17:34

+0

@ mosmic12:'pt'不是'temp.first'的別名,而是它的一個副本(稍後會被覆蓋)。 – Jarod42 2014-09-27 16:23:38

+0

@ mosmic12不,即使是這樣,你可以在'Poly :: operator +()'範圍內將它定義爲一個指針變量。 – Christophe 2014-09-27 16:31:38