問題描述
我正在創建一個雙鏈表,并重載了 operator= 以使列表中的另一個相等:
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
另外,如果有幫助,第 74 行是定義的最后一行,帶有}".
Also, if it helps, line 74 is the last line of the definition, with the "}".
推薦答案
正如它所說的:運算符重載必須是成員函數.(在類中聲明)
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)
{
...
}
另外,從 = 返回 LHS 可能是個好主意,這樣你就可以鏈接它(比如 a = b = c
) - 所以讓它list
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=....
這篇關于“operator = must be a non-static member"是什么意思?意思?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!