模板链表 Class 的复制构造函数错误:没有匹配函数来调用 'Node<int>::Node()'
Copy Constructor Error With a Template Linked List Class : no matching function for call to 'Node<int>::Node()'
我正在尝试为链表创建一个复制构造函数,但我不确定如何修复这个错误,我已经找了好几个小时了。错误是:
no matching function for call to '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;
}
class节点没有默认构造函数。它只有以下构造函数
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;
在循环的右大括号之前。
我正在尝试为链表创建一个复制构造函数,但我不确定如何修复这个错误,我已经找了好几个小时了。错误是:
no matching function for call to '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;
}
class节点没有默认构造函数。它只有以下构造函数
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;
在循环的右大括号之前。