简体   繁体   English

重载加法运算符

[英]overloading addition operator

I have a linked list that is represented as 我有一个链表,表示为

struct term{
    double coef;
    unsigned deg;
    struct term * next;
    };

then i have a polynomial class 那我有一个多项式类

class Polynomial{
public:
    Polynomial & operator+ (const Polynomial & ) const;
private:
    term *ptr

and i am trying to do an addition overloaded operator, but what i tried just give me some random part of the polynomial that is in the middle. 我试图做一个额外的重载运算符,但我尝试只是给我一些中间的多项式的随机部分。

Polynomial & Polynomial::operator+(const Polynomial & poly) const{
    Polynomial p2 = *this;
    term * temp = (*this).ptr;
    while(temp->next != NULL){
        temp = temp->next;
    }
    temp->next = poly.ptr;
    return p2;
}

and also when i have 2 polynomials, one is a copy of another just then added one more term, then when i try to use the addition operator, the first polynomial is bigger, like the second polynomial is added to it. 并且当我有2个多项式时,一个是另一个的副本然后再添加一个项,然后当我尝试使用加法运算符时,第一个多项式更大,就像第二个多项式被添加到它。

You're returning a temp by reference, which is undefined behavior. 您通过引用返回临时值,这是未定义的行为。 Return a Polynomial instead. 返回一个Polynomial Ie

Polynomial & operator+ (const Polynomial & ) const;

should be 应该

Polynomial operator+ (const Polynomial & ) const;

You're also missing a copy constructor and copy assignment operator . 您还缺少复制构造函数和复制赋值运算符

You're operator+() is seriously whacked. 你的操作员+()严重受到打击。 Consider the idea of "term" ownership. 考虑“期限”所有权的概念。 Each polynomial has a linked list of terms. 每个多项式都有一个链接的术语列表。 It owns this list (at least it better). 拥有这个列表(至少它更好)。 Now consider this brief analysis of your operator +() : 现在考虑对operator +() 简要分析:

Polynomial Polynomial::operator+(const Polynomial & poly) const 
{
    // hopefully creates a deep copy of *this
    Polynomial p2 = *this;

    // walk *our* terms (not the copy in p2??) to find the end.
    term * temp = (*this).ptr;
    while(temp->next != NULL)
        temp = temp->next;

    // once we find the end, we then *LINK* the params term-list
    //  which *they* are **supposed** to own, to our list. (and 
    //  p2 is still out there with a copy of our original content).
    temp->next = poly.ptr;

    // now we return p2, still holding a copy of our former self,
    // and we now have a cross-linked term list between us and the 
    // parameter poly
    return p2;
}

I sincerely hope it is evident what is wrong with that. 我真诚地希望这显然有什么不妥之处。 For this to work correctly, your operator should be returning a by-val, (which it is, hooray!), and manufacture that thing correctly: 为了使其正常工作,您的操作员应该返回一个by-val(它很好!),然后正确地制造它:

  • Make a copy (lets call it p2 ) of *this (you have that) 制作*这个(你有那个)的副本(让我们称之为p2
  • Find the end of the term list owned by p2 找到p2拥有的术语列表的结尾
  • Duplicate all terms in the rhs parameter of operator +(const Polynomial* rhs) , linking the copies one-by-one to the tail of p2 's term list. 复制operator +(const Polynomial* rhs)rhs参数中的所有项,将副本逐个链接到p2的术语列表的尾部。 Note: the tail will move with each new term linked. 注意:尾部将随着每个新术语的链接而移动。
  • Return p2 by val. 通过val返回p2。 If your copy-ctors and destructors are doing their job, everything should come out fine. 如果你的副本和破坏者正在做他们的工作,一切都应该好了。 When done, both *this, and rhs should be untouched. 完成后,* this和rhs都应该不受影响。

Thats about the extent I can offer. 多数民众赞成我可以提供的程度。 Good luck. 祝好运。

PS: For extra-credit-bonus-round, sort your terms in your list as you insert them. PS:对于额外信用额外奖励,请在插入时在列表中对术语进行排序。 This will get you one step closer to a like-degree merge, which will be the backbone of operator +=() and greatly assist your operator +() . 这将使您更接近类似度合并,这将是operator +=()的支柱,并极大地帮助您的operator +() The latter literally degenerates to Polynomial p2 = *this; p2 += poly; return p2; 后者字面上退化为Polynomial p2 = *this; p2 += poly; return p2; Polynomial p2 = *this; p2 += poly; return p2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM