创建新节点时出现问题

时间:2014-09-27 16:02:26

标签: c++ pointers linked-list

我试图为一个Poly类重载+运算符,所以在main.cpp中我可以一起添加到Poly。我试图这样做的方式是:

  1. 将左侧 expnt 与右侧 expnt
  2. 进行比较
  3. 使用新找到的值在 Poly temp 中创建一个新节点(在评论中说明)
  4. 然后将新的Poly返回到main.cpp以存储在pList []数组中。
  5. 由于某种原因,将创建新节点,但指向节点的指针会丢失,因此main.cpp无法访问 temp 。我哪里错了?

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

1 个答案:

答案 0 :(得分: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分配给它们的下一个元素时,你会面临一个类似的问题:它只对局部变量产生影响,对你的多边形没有影响。

顺便说一句,如果你试图添加多项式,请注意你只在这里处理第一个术语,而你没有正确处理其余的。

寻求解决方案的方式:

首次回归:

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

然后在return temp;之前,添加以下声明:

temp.first = pt; 

如果你要添加多项式,你应该考虑创建一个循环,不仅要处理第一个术语,还要处理其余的术语。