Search code examples
c++classoperator-overloadingnon-static

What does "operator = must be a non-static member" mean?


I'm in the process of creating a double-linked list, and have overloaded the operator= to make on list equal another:

template<class T>
void operator=(const list<T>& lst)
{
    clear();
    copy(lst);
    return;
}

but I get this error when I try to compile:

container_def.h(74) : error C2801: 'operator =' must be a non-static member

Also, if it helps, line 74 is the last line of the definition, with the "}".


Solution

  • Exactly what it says: operator overloads must be member functions. (declared inside the class)

    template<class T>
    void list<T>::operator=(const list<T>& rhs)
    {
        ...
    }
    

    Also, it's probably a good idea to return the LHS from = so you can chain it (like a = b = c) - so make it list<T>& list<T>::operator=....