-2

I give two lists l1, l2 with data [1,1,1] and [2,2,2], respectively.

While I do l1+=l2, it runs the program below.

But, while it's doing return *this, it will show segmentation fault:11

List List::operator+=(const List &other){
unsigned int min_len = (this->_len < other._len) ? _len : other._len;
  for (int i = 0; i < min_len; i++){
      this->_Array[i] += other._Array[i];
  }
  return *this;
}

I have no idea why it got this error.

However, if I change "return *this" to "return 0", it can work.

Another problem is that I try to change List to List&, it got another address problem.

1 Answers1

-1

Because you're returning the list by value. Declare your method like this:

List& List::operator+=(const List &other)

Hame
  • 494
  • 4
  • 18
  • Returning the `List` by value is ideologically wrong but will not be an error without help. As pointed out in the comments under the question, this is likely the result of [not properly observing the Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), but could be caused by many other problems. This question cannot be definitively answered unless the Asker provides more code. – user4581301 Oct 29 '17 at 16:39