模板链接列表类的复制构造函数错误:没有调用“Node::Node()”的匹配函数
我正在尝试为链接列表创建一个复制构造函数,但我不确定如何修复此错误,并且我已经寻找了几个小时。错误是:
没有调用“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;
}
I'm trying to make a copy constructor for a linked list but I'm not sure how to fix this error and I've been looking for hours. The error is:
no matching function for call to 'Node::Node()'
Here is the code:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Node 类没有默认构造函数。它只有以下构造函数
,并且在此语句中
使用了不存在的默认构造函数。
至少
您需要编写
而不是这三个语句。请注意,在 while 循环内,
指针
Curr
没有更改。因此,如果 Curr 不是空指针,则循环是无限的。您似乎忘记
在循环的右大括号之前插入此语句。
The class Node does not have the default constructor. It has only the following constructor
And in this statement
there is used the default constructor that is absent.
At least instead of these three statements
you need to write
Pay attention to that within the while loop
the pointer
Curr
is not changed. So the loop is infinite ifCurr
is not a null pointerIt seems you forgot to insert this statement
before the closing brace of the loop.