结构体定义

M0605050215 2009-03-13 01:21:56

template <class T>
struct Node
{
T data;
Node<T>* next;
};
与这样定义结构体有区别???
template <class T>
typedef struct
{
T data;
Node<T>* next;
}Node;

为什么我用后者出了很多错误,而前者没有报错,,,,
...全文
411 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我觉得模板与typedef之间有冲突,直接这么用
template <class T> struct LNode{
T data;//值域
LNode* next; //指针域
};
HelloDan 2009-03-13
  • 打赏
  • 举报
回复
template <class T>
typedef struct
{
T data;
Node* next; //这里之前没有Node的定义啊。
}Node;
nicky_zs 2009-03-13
  • 打赏
  • 举报
回复
你第二个定义里面,Node这个名字还没有定义出来,你在结构体里面就先使用它声明指针Node<T> *next了。。。 不出错难道编译器还会猜这个Node就是你现在正在定义的东东?
M0605050215 2009-03-13
  • 打赏
  • 举报
回复
同样是不行啊.....不知道为什么呢..
我把我代码贴部分上来....请大家指点!!!!

第二种形式的:
#include<iostream>
using namespace std;

template <class T>
typedef struct
{
T data;
Node* next;
}Node;

template <class T>
class LinkList
{
private:
Node<T> *first;
int length;
public:
LinkList() {first=NULL;length=0;}
LinkList(T *a,int n);
int Locate(T x);
int Del(T x);
void Insert(int i,T x);
T Get(int i);
int GetLength() {return length;}
ostream& Print(ostream&);
~LinkList();
};

错误如下:
一些慕名的错误,,,汗!!!
main.cpp
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(9) : error C2059: syntax error : '<end Parse>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(11) : error C2954: template definitions cannot nest
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2143: syntax error : missing ';' before '<'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2501: 'Node' : missing storage-class or type specifiers
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2059: syntax error : '<'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2238: unexpected token(s) preceding ';'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(18) : error C2501: 'first' : missing storage-class or type specifiers
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(18) : error C2501: 'length' : missing storage-class or type specifiers
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(19) : error C2146: syntax error : missing ';' before identifier 'LinkList'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(27) : see reference to class template instantiation 'LinkList<T>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(45) : error C2244: 'LinkList<T>::LinkList<T>::LinkList<T>' : unable to resolve function overload
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(47) : error C2954: template definitions cannot nest
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(48) : error C2039: 'Locate' : is not a member of 'LinkList<T>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(63) : error C2039: 'Get' : is not a member of 'LinkList<T>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(79) : error C2039: 'Del' : is not a member of 'LinkList<T>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(104) : error C2039: 'Insert' : is not a member of 'LinkList<T>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(131) : error C2039: 'Print' : is not a member of 'LinkList<T>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(146) : error C2039: '__dtor' : is not a member of 'LinkList<T>'
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(146) : error C2935: 'LinkList<T>' : template-class-id redefined as a global function
c:\documents and settings\administrator\桌面\test\linklist\main.cpp(35) : error C2593: 'operator >>' is ambiguous
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2143: syntax error : missing ';' before '<'
c:\documents and settings\administrator\桌面\test\linklist\main.cpp(36) : see reference to class template instantiation 'LinkList<struct DATA>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2501: 'Node' : missing storage-class or type specifiers
c:\documents and settings\administrator\桌面\test\linklist\main.cpp(36) : see reference to class template instantiation 'LinkList<struct DATA>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2059: syntax error : '<'
c:\documents and settings\administrator\桌面\test\linklist\main.cpp(36) : see reference to class template instantiation 'LinkList<struct DATA>' being compiled
c:\documents and settings\administrator\桌面\test\linklist\linklist.h(15) : error C2238: unexpected token(s) preceding ';'
c:\documents and settings\administrator\桌面\test\linklist\main.cpp(36) : see reference to class template instantiation 'LinkList<struct DATA>' being compiled
执行 cl.exe 时出错.

当我采用前者定义时就不是这样>....
#include<iostream>
using namespace std;

template <class T>
struct Node
{
T data;
Node* next;
};

template <class T>
class LinkList
{
private:
Node<T> *first;
int length;
public:
LinkList() {first=NULL;length=0;}
LinkList(T *a,int n);
int Locate(T x);
int Del(T x);
void Insert(int i,T x);
T Get(int i);
int GetLength() {return length;}
ostream& Print(ostream&);
~LinkList();
};
Compiling...
main.cpp

main.obj - 0 error(s), 0 warning(s)

HelloDan 2009-03-13
  • 打赏
  • 举报
回复

template <class T>
typedef struct abc
{
T data;
Node<T>* next;
}Node;
//这样试一下看看怎样

65,211

社区成员

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

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