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

user98188 picture user98188 · May 16, 2009 · Viewed 75.6k times · Source

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 "}".

Answer

v3. picture v3. · May 16, 2009

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=....