C++ 由于某种原因,我无法让模板在链表中工作,我敢打赌这是一个简单的错误,但我就是不明白
Posted
技术标签:
【中文标题】C++ 由于某种原因,我无法让模板在链表中工作,我敢打赌这是一个简单的错误,但我就是不明白【英文标题】:C++ I can't get templates to work in a linked list for some reason, I bet is a simple error but I just don't get it 【发布时间】:2017-02-13 00:53:54 【问题描述】:我必须创建一个与模板一起使用的链表,以便它可以获取任何类型的数据。这部分看起来很简单,但出于某种原因,我已经以所有可能的方式调整了我的代码以及我对整个模板的东西似乎还不够。
“LinkedList.h”
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
using namespace std;
template <class N>
class list
private:
template <class N>
struct node
N data;
node* next;
;
node<N>* head;
node<N>* curr;
node<N>* temp;
node<N>* tail;
public:
list();
void AddNode(N addData);
void DeleteNode(N delData);
void PrintList();
;
template <class N>
list<N>::list()
head = NULL;
curr = NULL;
temp = NULL;
tail = NULL;
template <class N>
void list<N>::AddNode(N addData)
node<N>* n = new node;
n->next=NULL;
n->data = addData;
if (head != NULL)
curr = head;
while (curr->next != NULL)
curr = curr->next;
curr->next = n;
else
head = n;
template <class N>
void list<N>::DeleteNode(N delData)
node<N>* delPtr = NULL;
temp = head;
curr = head;
while (curr != NULL && curr->data != delData)
temp = curr;
curr = curr->next;
if (curr == NULL)
cout << delData << " Was not in the list." << endl;
delete delPtr;
else
delPtr = curr;
curr = curr->next;
temp->next = curr;
if (delPtr == head)
head = head->next;
temp = NULL;
delete delPtr;
cout << "The value " << delData << " was deleted." << endl;
template <class N>
void list<N>::PrintList()
curr = head;
while (curr != NULL)
cout << curr->data << endl;
curr = curr->next;
#endif
主要只是测试功能是否真的有效。
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
int main()
list<int> a;
a.AddNode(3);
a.PrintList();
system("pause");
return 0;
但他们没有,我收到 17 个错误,所有这些都是没有意义的语法错误,例如 ; missing before < in line 1
等等。
请你能告诉我我做错了什么吗?我知道它必须是一些我看不到的简单的东西,因为我是新手,但这太令人沮丧了!
【问题讨论】:
当你得到一堆与你所说的相似的错误时(你能包括实际的错误吗?复制粘贴为文本?),总是看看 first 错误.转到该错误指示的行,该行是否看起来有问题?不?然后转到 previous(非空,无注释)行(错误消息中未报告)并查看您在那里执行的操作。如果上一行是#include
,则检查包含的头文件的结尾。
在AddNode
中,node<N>* n = new node;
行在一个地方使用node<N>
,但在另一个地方只使用node
。
【参考方案1】:
我尝试了你的代码,但我只得到了一个“阴影模板参数”,可以通过将你的节点声明移到链表声明之外来解决。然后在 list::AddNode(N) 你必须更正你的 n 声明。
我能够编译它,但我没有深入测试你的代码。
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
using namespace std;
template <class N>
struct node
N data;
node* next;
;
template <class N>
class list
private:
node<N>* head;
node<N>* curr;
node<N>* temp;
node<N>* tail;
public:
list();
void AddNode(N addData);
void DeleteNode(N delData);
void PrintList();
;
template <class N>
list<N>::list()
head = NULL;
curr = NULL;
temp = NULL;
tail = NULL;
template <class N>
void list<N>::AddNode(N addData)
node<N>* n = new node<N>; // previously: node<N>* n = new node;
n->next=NULL;
n->data = addData;
if (head != NULL)
curr = head;
while (curr->next != NULL)
curr = curr->next;
curr->next = n;
else
head = n;
template <class N>
void list<N>::DeleteNode(N delData)
node<N>* delPtr = NULL;
temp = head;
curr = head;
while (curr != NULL && curr->data != delData)
temp = curr;
curr = curr->next;
if (curr == NULL)
cout << delData << " Was not in the list." << endl;
delete delPtr;
else
delPtr = curr;
curr = curr->next;
temp->next = curr;
if (delPtr == head)
head = head->next;
temp = NULL;
delete delPtr;
cout << "The value " << delData << " was deleted." << endl;
template <class N>
void list<N>::PrintList()
curr = head;
while (curr != NULL)
cout << curr->data << endl;
curr = curr->next;
#endif
然后在 main.cpp
#include <iostream>
#include <cstdlib>
#include "ll.hpp"
int main()
list<int> a;
a.AddNode(3);
a.AddNode(5);
a.AddNode(1);
a.PrintList();
a.DeleteNode(4);
a.PrintList();
a.AddNode(10);
a.DeleteNode(3);
a.PrintList();
//system("pause");
return 0;
输出为
3
5
1
4 Was not in the list.
3
5
1
The value 3 was deleted.
5
1
10
【讨论】:
以上是关于C++ 由于某种原因,我无法让模板在链表中工作,我敢打赌这是一个简单的错误,但我就是不明白的主要内容,如果未能解决你的问题,请参考以下文章