0

I'm trying to write an overloaded assignment operator for my linked list class using a template but I keep getting errors.

Any help with what I'm doing wrong would be great.

The error I'm getting is "out of line definition does not match any declaration in LL".

The declaration I have is:

const LL<T>& operator=(const LL<T> &rhsObj);

and the implementation is:

template<typename T>
LL<T>& LL<T>::operator=(const LL<T>& rhsObj) const
{
    if (this!= &rhsObj)
    {
        //no self assignment so OK to continue
        //deallocate left hand side memory
        this->clear();

        count = 0;
        head = NULL;

        cout <<"calling function copyList()" << endl;

        count = 0;
        head = NULL;

        string diCode = "";
        int onNode = 0;

        if(rhsObj.head == NULL)
        {
            cout <<"other list is empty, nothing to do" << endl;

        }
        else
        {
            onNode =0;
            Node<T> *otherCurrent = rhsObj.head;
            while( otherCurrent != NULL)
            {
                int duplicateInfo;
                duplicateInfo = otherCurrent->info;

                push_back(duplicateInfo);

                otherCurrent = otherCurrent ->next;
                onNode++;

            } //END while(otherCurrent != NULL)

        } // END else block of if (otherLL.head == NULL)

    } // END if (this != &rhsObj)

    return *this;
}
Anna Goldberg
  • 113
  • 1
  • 8
  • 4
    The assignment member function shouldn't be const. – Anon Mail Nov 02 '15 at 18:51
  • Are you splitting you template class in a .h and .cpp file? If so see: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – NathanOliver Nov 02 '15 at 18:51
  • 1
    `LL::operator=` needs to be `LL::operator=` – R Sahu Nov 02 '15 at 18:52
  • I've implemented R Sahu's suggestion but Im still getting errors, I've changed the question to reflect my errors. Any and all help, thanks :) – Anna Goldberg Nov 03 '15 at 02:26
  • Function definition doesn't match function declaration. **LL& operator=(const LL& rhsObj) const** and **const LL& operator=(const LL& rhsObj)** aren't same. Actually, assignment operator can't be **const**, as **Anon Mail** said. – Michael Nastenko Nov 03 '15 at 04:55

1 Answers1

0

In the updated code, the error is because your declaration is:

const LL<T>& operator=(const LL<T> &rhsObj);

but the attempted implementation is:

LL<T>& LL<T>::operator=(const LL<T>& rhsObj) const

The location of const is actually significant. The first one means that the function returns a reference which is const; the second one means you may not modify *this within the function.

A trailing const is part of the function signature, so the compiler does not consider your implementation to correspond to the function that was declared initially. So it looks like you're trying to add a new function that wasn't declared in the class definition, which isn't permitted.

To fix this, make the implementation match the declaration (move the const from the end to the start).

M.M
  • 138,810
  • 21
  • 208
  • 365