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

黑夜愁客 2009-03-09 11:14:40
#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 个 ==========


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


...全文
117 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sagegz 2009-03-09
  • 打赏
  • 举报
回复
这样的问题在坛子里已经说过很多次了,将模板的定义和实现都放.h文件中去.
模板不支持把函数声明和定义分开到2个文件中.
pengzhixi 2009-03-09
  • 打赏
  • 举报
回复
jf
zhkefa 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 healer_kx 的回复:]
纯接分。
[/Quote]

**
grellen 2009-03-09
  • 打赏
  • 举报
回复
接分
  • 打赏
  • 举报
回复
..好快,结果处得真快.
恭喜楼主.
healer_kx 2009-03-09
  • 打赏
  • 举报
回复
纯接分。
lightnut 2009-03-09
  • 打赏
  • 举报
回复
1. 将模板的定义和实现都放.h文件中去.
2. insert(header->next, word[j]);
===>
header->insert(header->next, word[j]);

insert(header, word[j]);
====>
header->insert(header, word[j]);

writeLinkedList(header);
=====>
header->writeLinkedList(header);

clear(header);
======>
header->clear(header);

fox000002 2009-03-09
  • 打赏
  • 举报
回复
知道就好,顺道 jf
黑夜愁客 2009-03-09
  • 打赏
  • 举报
回复
晕,我知道我自己错在哪里啊

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧