0

I have the following function/constructor/method (I'm not sure exactly what it is)

List<T>& List<T>::operator=(const List<T> &x)

where List is a linked list and this is supposed to do assignment. However, I'm not sure exactly what this is supposed to return. Eclipse keeps telling me that control reaches end of non-void function; however, I'm not sure exactly what. I'm new to C++ so keep the answers simple if posssible. Thanks :)

Nosrettap
  • 10,940
  • 23
  • 85
  • 140
  • You're not returning anything. You need to return something (in this case, probably the new List object). – Platinum Azure Jan 30 '12 at 04:55
  • Also read [What is the Copy-and-Swap Idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) which describes best practices for writing a custom assignment operator. – Ben Voigt Jan 30 '12 at 05:05
  • `operator==` is not a constructor, because the object you assign to already exists. – fredoverflow Jan 30 '12 at 10:14

2 Answers2

3

Usually, assignment operators return a reference to the object itself, so just end your function with return *this;. Having an assignment expression have the value of the assignee allows you to write things like a = b = c; and if ((a = get_data()) == c) etc.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

It is an overloaded = operator to your List<T> class, It should return a reference to the class type. Returning a reference allows the return value to act as an l-value and allows its usage in chaining calls like:

a = b = c;

You should be returning a *this.

Explanation of what it is:

List<T>& List<T>::operator = (const List<T> &x)
^^^^^^^  ^^^^^^^  ^^^^^^^^ ^  ^^^^^^^^^^^^^^^^ <------------- Parameter being passed to =
Return Type |         |    |
            |         |    |<------------- operator being overloaded
            |       Keyword operator
         Class whose operator is being overloaded   
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Unfortunately I could only give 1 accepted answer, otherwise I would have given you one. However, I up voted you to show my appreciation, anyways...cheers – Nosrettap Jan 30 '12 at 05:19
  • @Nosrettap: No worries :) Always accept the answer which helps you in best possible way to solve your problem. – Alok Save Jan 30 '12 at 05:22