我對C++模板非常陌生。我目前正在開發一個項目,我需要使用模板來實現一個雙鏈表。這是目前我到目前爲止有:(C++)雙鏈表列表模板 - 訪問問題
template<class ItemType>
class SortedList
{
public:
SortedList();
~SortedList();
bool Insert (ItemType toAdd);
bool Delete (ItemType toDelete);
void Print();
private:
SortedList (const SortedList & copyFrom);
SortedList & operator= (const SortedList & assignFrom);
struct Node
{
Node (ItemType item, Node * p = NULL, Node * n = NULL)
{ data = item; prev = p; next = n; }
ItemType data;
Node * prev, * next;
};
Node * list;
};
template<class ItemType>
SortedList<ItemType>::SortedList()
{
list == NULL;
}
template<class ItemType>
SortedList<ItemType>::~SortedList()
{
Node * curr = list;
while (curr != NULL)
{
Node * tempNext = curr->next;
delete current;
current = tempNext;
}
}
然而,在我的析構函數,例如,爲什麼我不能訪問節點元素?現在編譯該方法內的代碼,但不會拋出錯誤。但是,如果我嘗試使用 - >在curr上,next或prev不會出現。爲什麼我無法訪問這些?我覺得我缺少一些非常明顯的東西來開始。
另外,我怎麼能初始化函數頭中的列表== NULL,而不是在類之外呢?
有在構造一個錯字。它應該是list = NULL;你不使用==來初始化,而是進行比較。 – 2014-10-28 05:39:59