类模板"class has no constructors"错误

"class has no constructors" error for class template

本文关键字:no constructors 错误 has class      更新时间:2023-10-16

我正在制作一个链表,我只想在列表的前面添加一个节点。 我做错了什么?

Node.h

 #pragma once
 namespace list_1
 {
template <typename T>
struct Node
{
    T data;
    Node<T> *next;
    // Constructor
    // Postcondition: 
    Node<T> (T d);
};
template <typename T>
Node<T>::Node(T d)
{
    data = d;
    next = NULL;
}
 }

list.h

template <typename T>
void list<T>::insert_front(const T& entry)
{
    Node<T> *temp = head;
    if(temp == NULL)
        temp->next = new Node(entry);
    else
    {
        while (temp->next != NULL)
        {
           temp = temp->next;
        }
        temp->next = new Node(entry);
    }
}

错误信息;

 1>------ Build started: Project: Linked List, Configuration: Debug Win32 ------
 1>  list_test.cpp
 1>c:...linked listlist.h(54): error C2955: 'list_1::Node' : use of class template requires template argument list
 1>          c:...linked listnode.h(7) : see declaration of 'list_1::Node'
 1>          c:...linked listlist.h(48) : while compiling class template member function 'void list_1::list<T>::insert_front(const T &)'
 1>          with
 1>          [
 1>              T=double
 1>          ]
 1>          c:...linked listlist_test.cpp(33) : see reference to class template instantiation 'list_1::list<T>' being compiled
 1>          with
 1>          [
 1>              T=double
 1>          ]
 1>c:...linked listlist.h(54): error C2514: 'list_1::Node' : class has no constructors
 1>          c:...linked listnode.h(7) : see declaration of 'list_1::Node'
 1>c:...linked listlist.h(62): error C2514: 'list_1::Node' : class has no constructors
 1>          c:...linked listnode.h(7) : see declaration of 'list_1::Node'
 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

阅读错误消息:

使用类模板需要模板参数列表

new Node(entry)应该new Node<T>(entry)