类模板问题

zyf19830914 2009-06-09 03:39:56
三个函数:
1.Node.h
#ifndef NODE_H
#define NODE_H
using namespace std;
template<class T>
class Node
{
public:
T data;
Node<T>* next;

Node()
{
next=NULL;
}

Node(T data,Node<T> *next=NULL)
{
this->data=data;
this->next=next;
}
};
#endif

2.singlyLinkedList.h

#ifndef NODE_H
#define NODE_H
#include"Node.h"
#include<iostream>
using namespace std;
template<class T> class Node;
template<class T>
class singlyLinkedList
{
public:
Node<T>* head;
singlyLinkedList();
singlyLinkedList(T value[],int n);
~singlyLinkedList();
bool isEmpty();
int length();
Node<T>* getNode(int i);
T get(int i);
bool set(int i,T x);
friend ostream& operator<<(ostream& out,singlyLinkedList<T>& list)
{
Node<T>* p=list.head;
out<<"(";
while(p!=NULL)
{
out<<p->data;
p=p->next;
if(p!=NULL)
out<<",";
}
out<<")";
return out;
}
Node<T>* insert(int i,T x);
bool remove(int i,T& old);
void clear();
void concat(singlyLinkedList<T> &list);

};

template<class T>
singlyLinkedList<T>::singlyLinkedList()
{
head=NULL;
}

template<class T>
singlyLinkedList<T>::singlyLinkedList(T value[],int n)
{
head=NULL;
if(n>0)
{
head=new Node<T>(value[0]);
Node<T>* rear=head;
int i=1;
for(i=1;i<n;++i)
{
rear->next=new Node<T>(value[i]);
rear=rear->next;
}

}
}

template<class T>
singlyLinkedList<T>::~singlyLinkedList()
{
clear();
}

template<class T>
bool singlyLinkedList<T>::isEmpty()
{
return head==NULL;
}

template<class T>
int singlyLinkedList<T>::length()
{
int i=0;
Node<T>* p=head;
while(p!=NULL)
{
++i;
p=p->next;
}
return i;
}

template<class T>
Node<T>* singlyLinkedList<T>::getNode(int i)
{
if(i<0)
return NULL;
int j=0;
Node<T>* p=head;
while(p!=NULL&&j!=i)
{
++j;
p=p->next;
}
return p;
}

template<class T>
T singlyLinkedList<T>::get(int i)
{
Node<T>* p=getNode(i);
if(p!=NULL)
return p->data;
throw "指定序号无效";
}

template<class T>
bool singlyLinkedList<T>::set(int i,T x)
{
Node<T>* p=getNode(i);
if(p!=NULL)
{
p->data=x;
return true;
}
return false;
}

template<class T>
void singlyLinkedList<T>::clear()
{
Node<T>* p=head;
while(p!=NULL)
{
Node<T>* q=p;
p=p->next;
delete q;
}
head=NULL;
}

template<class T>
bool singlyLinkedList<T>::remove(int i,T& old)
{
if(i==0)
{
Node<T>* q=head;
old=q->data;
head=head->next;
delete q;
return true;
}
Node<T>* p=getNode(i-1);
if(p!=NULL)
{
Node<T>* q=p->next;
old=q->data;
p->next=q->next;
delete q;
return true;
}
return false;
}

template<class T>
Node<T>* singlyLinkedList<T>::insert(int i,T x)
{
Node<T>* q=NULL;
if(head=NULL||i<=0)
{
q=new Node<T>(x,head);
head=q;
}
else
{
int j=0;
Node<T>* p=head;
while(p->next!=NULL&&j<i-1)
{++j;p=p->next;}
q=new Node<T>(x,p->next);
p->next=q;
}
return q;
}

template<class T>
void singlyLinkedList<T>::concat(singlyLinkedList<T> &list)
{
if(this->head==NULL)
this->head=list.head;
else
{
Node<T>* p=head;
while(p->next!=NULL)
p=p->next;
p->next=list.head;
}
list.head=NULL;
}
#endif

3.main.cpp
#include<iostream>
#include"singlyLinkedList.h"
using namespace std;
int main()
{
singlyLinkedList<char> lista("abc",3);
singlyLinkedList<char> listb("xy",2);
lista.concat(listb);
cout<<listb<<lista;
return 0;
}


一下是编译错误:
Compiling...
main_singleLinkedList.cpp
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(26) : error C2027: use of undefined type 'Node<char>'
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl singlyLinkedList<char>::o
perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class singlyLinkedList<char> &)'
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(26) : error C2227: left of '->data' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl singlyLinkedList<char>::o
perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class singlyLinkedList<char> &)'
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(27) : error C2027: use of undefined type 'Node<char>'
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl singlyLinkedList<char>::o
perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class singlyLinkedList<char> &)'
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(27) : error C2227: left of '->next' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl singlyLinkedList<char>::o
perator <<(class std::basic_ostream<char,struct std::char_traits<char> > &,class singlyLinkedList<char> &)'
Error executing cl.exe.
Creating browse info file...

DS_singlyLinkedList.exe - 4 error(s), 0 warning(s)

有没有会的帮我看看
...全文
27 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
steven_007 2009-06-30
  • 打赏
  • 举报
回复
学习。
zyf19830914 2009-06-30
  • 打赏
  • 举报
回复
是的啊,谢谢楼上的提醒,太感谢了
goodname 2009-06-09
  • 打赏
  • 举报
回复
你的这两个.h文件的宏包含写成一样的了,这样对于main.cpp造成了第一个.h文件实效了

64,654

社区成员

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

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