[英]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(它很好!),然后正确地制造它:
p2
) of *this (you have that) p2
) p2
p2
拥有的术语列表的结尾 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. rhs
should be untouched. 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.