我正在创建一个链表,我只想在列表的前面添加一个节点。我做错了什么?
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 ==========
答案 0 :(得分:7)
阅读错误消息:
使用类模板需要模板参数列表
new Node(entry)
应为new Node<T>(entry)