0
我正在嘗試使用模板爲我的鏈接列表類編寫重載賦值運算符,但我不斷收到錯誤。使用重載運算符模板時出現錯誤=
任何幫助我做錯了會很好。
我收到的錯誤是「行外定義不符合LL中的任何聲明」。
聲明我是:
const LL<T>& operator=(const LL<T> &rhsObj);
和實現是:
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;
}
賦值成員函數不應該是const。 –
您是否在.h和.cpp文件中分割您的模板類?如果是這樣,請參閱:http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – NathanOliver
'LL :: operator ='需要是'LL: :operator =' –