模板链接列表类的复制构造函数错误:没有调用“Node::Node()”的匹配函数

发布于 2025-01-12 15:21:54 字数 1771 浏览 0 评论 0原文

我正在尝试为链接列表创建一个复制构造函数,但我不确定如何修复此错误,并且我已经寻找了几个小时。错误是:

没有调用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2025-01-19 15:21:54

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;

在循环的右大括号之前插入此语句。

The class Node does not have the default constructor. It has only the following constructor

Node(T Data)    //assign data value
{
    data = Data;    //makes data = NV parameter
    next = nullptr; //makes next = to nullptr
}

And in this statement

Node<T>* newCpy = new Node<T>;

there is used the default constructor that is absent.

At least instead of these three statements

//val = copyHead->data;
Node<T>* newCpy = new Node<T>;
newCpy->data = Curr->data;
newCpy->next = nullptr;

you need to write

//val = copyHead->data;
Node<T>* newCpy = new Node<T>( Curr->data );

Pay attention to that within the while loop

while(Curr)
{
    //...
}

the pointer Curr is not changed. So the loop is infinite if Curr is not a null pointer

It seems you forgot to insert this statement

Curr = Curr->next;

before the closing brace of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文