模板写的双链表结构-出错

黑夜愁客 黑夜愁客 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#ifndef NOTE_H
#define NOTE_H

template <typename T>
class Note{
public:
T nodeValue;
Note<T> *prev;
Note<T> *next;

Note(){
next = this;
prev = this;
}

Note(const T &value):nodeValue(value){
next = this;
prev = this;
}

void writeLinkedList(Note<T> *header, const string& separator);

void erase(Note<T> *curr);

Note<T> *insert(Note<T> *curr, const T &item);

void clear(Note<T> *header);
};
#endif


文件实现:
#include <iostream>
#include <string>

#include "note.h"
using namespace std;

template <typename T>
void Note<T>::writeLinkedList(Note<T> *header, const string& separator = " "){
Note<T> *p = header->next;//header points at the first note, p moves through the list

while(p != header){
cout << p->nodeValue << separator;//we need to change this~
p = p->next;
}
}

template <typename T>
void Note<T>::erase(Note<T> *curr){
//return if current is empty
if(curr->next == curr)
return;

//declare pointers for the predecessor and successor nodes
Note<T> *prevNode = curr->prev;
Note<T> *succNode = curr->next;

//update the pointers for predecessor and successor
prevNode->next = succNode;
succNode->prev = prevNode;

delete curr;//free memory
}

template <typename T>
Note<T>* Note<T>::insert(Note<T> *curr, const T &item){
//declare pointers variable for the new node and the previous node
Note<T> *newNode,*prevNode;

//allocate a default node
newNode = new Note<T>(item);

//make prevNode points to the previous node of current one
prevNode = curr->prev;

//update the newNode's pointers
newNode->prev = prevNode;
newNode->next = curr;

//update the current node and previous node's pointers
//==>points to the new node
prevNode->next = newNode;
curr->prev = newNode;

return newNode;

}

template <typename T>
void Note<T>::clear(Note<T> *header){
while(header->next != header)
erase(header->next);
}


测试:


#include <iostream>
#include <string>

#include "note.h" // node class


using namespace std;



int main()
{
// header node for list holding jumbled characters
Note<char> *header = new Note<char>;
string word;
int numWords, i, j;
//randomNumber rnd;

// prompt for the number of words to enter
cout << "How many words will you enter? ";
cin >> numWords;
cout << endl;

for (i = 0; i < numWords; i++)
{
cout << "Word: ";
cin >> word;

// use rnd.random(2) to determine if the char is inserted
// at the front (value = 0) or back (value = 1) of the list
for (j = 0; j < word.length(); j++)
if (rand()%10 == 0)
// insert at the front of the list
insert(header->next, word[j]);
else
// insert at the back of the list
insert(header, word[j]);

// output the word and its jumbled variation
cout << "Word/Jumbled Word: " << word << " ";
writeLinkedList(header);
cout << endl << endl;

// clear the list in preparation for the next word
clear(header);
}

return 0;
}


错误很多:看了很多tempalte的使用,就是书上这里例子我按自己的方式写的:
note.cpp
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(21) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(28): 参见对正在编译的类 模板 实例化“Note<T>”的引用
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(21) : error C2143: 语法错误 : 缺少“,”(在“&”的前面)
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.cpp(15) : error C2244: “Note<T>::writeLinkedList”: 无法将函数定义与现有的声明匹配
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(21) : 参见“Note<T>::writeLinkedList”的声明
定义
'void Note<T>::writeLinkedList(Note<T> *,const std::string &)'
现有声明
'void Note<T>::writeLinkedList(Note<T> *,const int)'
test.cpp
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(21) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(28): 参见对正在编译的类 模板 实例化“Note<T>”的引用
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\note.h(21) : error C2143: 语法错误 : 缺少“,”(在“&”的前面)
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\test.cpp(33) : warning C4018: “<”: 有符号/无符号不匹配
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\test.cpp(36) : error C3861: “insert”: 找不到标识符
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\test.cpp(39) : error C3861: “insert”: 找不到标识符
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\test.cpp(43) : error C3861: “writeLinkedList”: 找不到标识符
c:\documents and settings\admin\my documents\visual studio 2008\projects\ipv6\ipv6\test.cpp(47) : error C3861: “clear”: 找不到标识符
正在生成代码...
生成日志保存在“file://c:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\Ipv6\Ipv6\Debug\BuildLog.htm”
Ipv6 - 9 个错误,1 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


很奇怪,不能找出其中原因,请大家帮忙下,谢谢


...全文

相关推荐

C#数据结构 算法 包含项目内容

很好的书籍 这门课程的目的有三个。第一个是讲授常用的数据结构,这些 数据结构形成了程序员基本数据结构工具箱(toolkit)。对于许多常见的问题,工 具箱里的数据结构是理想的选择。就像.NET Framework中Windows应用程序开 发中的工具箱,程序员可以直接拿来或经过少许的修改就可以使用,非常方便。 第二个是讲授常用的算法,这和数据结构一样,是人们在长期实践过程中的总结, 程序员可以直接拿来或经过少许的修改就可以使用。可以通过算法训练来提高程 序设计水平。第三个目的是通过程序设计的技能训练促进程序员综合能力的提 高。

fnchenlei

数据结构1800题——考研数据结构非常有用的参考书之一

想必计算机考研的人都知道吧!数据结构1800题是计算机考研人必不可少的一本参考书!里面包含题目与答案!希望对大家有帮助!

success041000

Python实现循环链表数据结构(包含详细的完整的程序和数据)

本文详细介绍了循环链表这一数据结构的两种形式—循环单链表与循环双链表的具体实现方法,并给出了具体的操作实例。内容涉及节点的添加与删除,查找元素等功能以及如何计算列表的长度。 适合人群:具有Python基础知识并对数据结构有一定兴趣的学习者与程序员。 使用场景及目标:适用于约瑟夫问题,循环队列等问题,通过学习能够更好地理解和运用这类结构来优化程序。 其他说明:强调正确的指针管理与合理的错误检查对于防止运行时发生故障(如内存泄漏和断开链表)是非常重要的,并提出可能进一步提升性能的方向。

nantangyuxi

C语言人事管理(附加流程图).docx

C语言人事管理(附加流程图).docx

zzzzl333

《数据结构(C语言版)》配书光盘.DSDemo.[XiDong.Net]

《数据结构(C语言版)》配书光盘.DSDemo.[XiDong.Net],对于理解算法很有帮助。。。。。。。

xidianyejijie
评论 9
收藏 已收藏
分享
返回顶部
黑夜愁客
C++ 语言 65,214 成员 250,514 内容