3

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

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 list\list.h(54): error C2955: 'list_1::Node' : use of class template requires template argument list
 1>          c:\...\linked list\node.h(7) : see declaration of 'list_1::Node'
 1>          c:\...\linked list\list.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 list\list_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 list\list.h(54): error C2514: 'list_1::Node' : class has no constructors
 1>          c:\...\linked list\node.h(7) : see declaration of 'list_1::Node'
 1>c:\...\linked list\list.h(62): error C2514: 'list_1::Node' : class has no constructors
 1>          c:\...\linked list\node.h(7) : see declaration of 'list_1::Node'
 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

1 回答 1

7

阅读错误信息:

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

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

于 2013-01-26T08:02:40.410 回答