我正在嘗試為鏈表創建一個復制建構式,但我不確定如何修復這個錯誤,我一直在尋找幾個小時。錯誤是:
沒有匹配函式呼叫'Node::Node()'
這是代碼:
template <class T>
class Node //node is the name of class, not specific
{
public:
T data; //t data allows for any type of variable
Node *next; //pointer to a node
Node(T Data) //assign data value
{
data = Data; //makes data = NV parameter
next = nullptr; //makes next = to nullptr
}
};
template <class T>
class LinkedList
{
private:
Node<T> * head, *tail; //// typedef Node* head // -- head = Node* -- //// typedef Node* nodePtr = Node* ////
public:
LinkedList() //constructor for Linked List
{
head = nullptr;
tail = nullptr;
}
LinkedList(LinkedList& copy)
{
head = nullptr;
tail = nullptr;
Node<T>* Curr = copy.head;
while(Curr) //while not at end of list
{
//val = copyHead->data;
Node<T>* newCpy = new Node<T>;
newCpy->data = Curr->data;
newCpy->next = nullptr;
if(head == nullptr)
{
head = tail = newCpy;
}
else
{
tail->next = newCpy;
tail = newCpy;
}
}
}
~LinkedList()
{
while (head)
{
Node<T>* tmp = head;
head = head->next;
delete(tmp);
}
head = nullptr;
}
uj5u.com熱心網友回復:
類 Node 沒有默認建構式。它只有以下建構式
Node(T Data) //assign data value
{
data = Data; //makes data = NV parameter
next = nullptr; //makes next = to nullptr
}
在這個宣告中
Node<T>* newCpy = new Node<T>;
使用了不存在的默認建構式。
至少代替這三個陳述
//val = copyHead->data;
Node<T>* newCpy = new Node<T>;
newCpy->data = Curr->data;
newCpy->next = nullptr;
你需要寫
//val = copyHead->data;
Node<T>* newCpy = new Node<T>( Curr->data );
注意while回圈中的那個
while(Curr)
{
//...
}
指標Curr沒有改變。Curr因此,如果不是空指標,則回圈是無限的
您似乎忘記插入此陳述句
Curr = Curr->next;
在回圈的右大括號之前。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/440066.html
上一篇:SSIS64位資料流組件
