我在我的代碼上找到解決方案時遇到問題,它運行正常,但是當我編輯 main 以運行代碼時,可能會有一些變化。所以現在我的代碼出現了這個錯誤:成員函式“isEmpty”的這個引數的型別為“const CircularLinkedList”,但函式未標記為 const
這是彈出錯誤的代碼部分。
//Copy Constructor
template <typename T>
CircularLinkedList<T>::CircularLinkedList(const CircularLinkedList& c1)
{
if (!c1.isEmpty())
{
Node<T>* curr = c1.frst;
//Iterating through elements until we encounter home node again
while (curr->next != first)
{
insert(curr->data);
curr = curr->next;
}
}
}
下面的代碼我最初沒有在我的主選單上運行代碼,但是一旦我把它放在上面,就會彈出錯誤。我不確定這是否與它有關。但作為參考這里是我添加的代碼。在此代碼之前我沒有任何錯誤。
int printMenu();
// InsertList inserts an item into the list parameter
void insertListItem ( CircularLinkedList<int> & );
// deletes the first occurrence from the list parameter
void deleteOne ( CircularLinkedList<int> & );
// deletes all the occurrence from list parameter
void deleteAll ( CircularLinkedList<int> & );
//return the length of the list
int totalCount( CircularLinkedList<int> & );
// searchItem searches for an item in the list parameter
void searchItem ( CircularLinkedList<int> );
// return the number of occurrences of a given item
int totalOccurence (CircularLinkedList<int> & );
uj5u.com熱心網友回復:
該錯誤是不言自明的:在您的CircularLinkedList<T>::CircularLinkedList復制建構式中,引數標記為const。
您只能使用其定義為的方法const。因此,將您的isEmpty定義更改為類似
bool isEmpty() const {...}
畢竟,檢查串列是否為空不應修改該串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/446762.html
