2014-09-27 72 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