eaglesky
eaglesky

Reputation: 720

'No match' error when overloading multiple operators

I tried to overload the '+' operator and '=' operator like below:

template<class T>
class LinkedList {     
 public:
  LinkedList();
  LinkedList(const LinkedList<T> &list);        //Copy Constructor! Allocating new memory!
  ~LinkedList();
  void PushNode(T new_data);
  void Delete(LinkedNode<T> *pnode);
  bool CopyDelete(LinkedNode<T> *pnode);
  bool CopyDeleteMiddle(); 
  void Show();
  int get_length();
  void Clear();
  LinkedList<T> operator+(LinkedList<T> &list);  //Overloading '+' operator
  LinkedList<T> &operator=(LinkedList<T> &list); //Overloading '=' operator
  LinkedNode<T> *head;
  LinkedNode<T> *tail;

 private:
  int length;
};

template <class T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList<T> &list)
{
  if (&list != this) {
    Clear();
    LinkedNode<T> *pnode = list.head;
    while(pnode)
    {
      PushNode(pnode->data);
      pnode = pnode->next;
    }
  }
  return *this;
}

template <class T>
LinkedList<T> LinkedList<T>::operator+(LinkedList<T> &list)
{
  int carry = 0;
  LinkedList<T> sum_list;
  LinkedNode<T> *pnode = list.head;
  LinkedNode<T> *pnode_this = head;
  if (!pnode || !pnode_this)
    throw("Invalid list!");

  T digit, digit_this, digit_sum;
  while(pnode || pnode_this)
  {
    if (!pnode) {
      digit = 0;
      digit_this = pnode_this->data;
      pnode_this = pnode_this->next;
    } else if (!pnode_this) {
      digit = pnode->data;
      digit_this = 0;
      pnode = pnode->next;
    } else {
      digit = pnode->data;
      digit_this = pnode_this->data;
      pnode = pnode->next;
      pnode_this = pnode_this->next;
    }
    digit_sum = digit + digit_this + carry;
    if (digit_sum >=10) {
      digit_sum-=10;
      carry = 1;
    } else {
      carry = 0;
    }
    sum_list.PushNode(digit_sum);

  }
  return sum_list;
}

In my main function, I created three objects like this:

LinkedList<int> list1;
LinkedList<int> list2;
LinkedList<int> sum;

And then I added the two lists and stored the result in sum:

sum = list1+list2; //error occurred here!

All my declarations and definitions about my LinkedList class are in MySinglyLinkedList.h, and my main function is in 2_4.cc.

I compiled 2_4.cc using GCC 4.5.2 in Linux(Ubuntu), and encountered the following error:

2_4.cc: In function ‘int main(int, char**)’:

2_4.cc:39:15: error: no match for ‘operator=’ in ‘sum = LinkedList<T>::operator+(LinkedList<T>&) [with T = int](((LinkedList<int>&)(& list2)))

MySinglyLinkedList.h:171:16: note: candidate is: LinkedList<T>& LinkedList<T>::operator=(LinkedList<T>&) [with T = int]

Is there anything wrong with my overloaded functions?

Upvotes: 0

Views: 149

Answers (2)

StackedCrooked
StackedCrooked

Reputation: 35485

The reference arguments:

LinkedList<T>& LinkedList<T>::operator=(LinkedList<T> &list)
LinkedList<T> LinkedList<T>::operator+(LinkedList<T> &list)

Should be changed to a const-ref:

LinkedList<T>::operator=(const LinkedList<T> &list)
LinkedList<T> LinkedList<T>::operator+(const LinkedList<T> &list)

As for operator= it's even better to pass by value and make use of the copy-swap idiom:

LinkedList<T>::swap(LinkedList<T> & rhs) {
    using std::swap;
    swap(head, rhs.head);
    swap(tail, rhs.tail;
}

LinkedList<T>& LinkedList<T>::operator=(LinkedList<T> list) {
    this->swap(list);
    return *this;
}

NOTE: this requires that the copy constructor is correctly implemented.

Upvotes: 1

Yexo
Yexo

Reputation: 1885

The argument tot operator= should be a const reference. A temporary (the result of operator+) can't be bound to a non-const reference.

Upvotes: 1

Related Questions