| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1199 人关注过本帖
标题:单链表问题
只看楼主 加入收藏
梦自在
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2007-3-26
收藏
 问题点数:0 回复次数:11 
单链表问题

请大虾们帮忙修改下,谢谢
#include<iostream.h>
template<class T>
struct Node
{
T data;
Node *next;
};
template<class T>
class LinkList
{
public:
LinkList();
LinkList(T [],int);
void Count(T);
private:
Node *first;
};
template<class T>
LinkList<T>::LinkList(T a[],int n)
{
Node *first=new Node;
Node *r=NULL;
r=first;
for(int i=0;i<n;i++)
{
Node *s=new Node;
s->data=a[i];
r->next=s;
r=s;
}
r->next = 0;
}
template<class T>
void LinkList<T>::Count(T x)
{
Node *p=first;
int count=0;
while(p->next)
{
if(p->data>x)
count++;
p=p->next;
}
cout<<count<<endl;
}
void main()
{
int n1;
cout<<"输入A的个数:n1=";
cin>>n1;
int *a;
a=new int[n1];
cout<<"\t输入A的数:";
for(int i=0; i<n1; i++)
cin>>a[i];
LinkList<int> A(a,n1);
int X;
cout<<"\t输入要比较的数X:";
cin>>X;
cout<<"比X大的数个个数为:";
A.Count(X);
}
运行时::\编程\C++\自编\001\001.cpp(21) : error C2955: 'Node' : use of class template requires template argument list
E:\编程\C++\自编\001\001.cpp(7) : see declaration of 'Node'
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
E:\编程\C++\自编\001\001.cpp(21) : error C2955: 'Node' : use of class template requires template argument list
E:\编程\C++\自编\001\001.cpp(7) : see declaration of 'Node'
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
E:\编程\C++\自编\001\001.cpp(21) : error C2512: 'Node' : no appropriate default constructor available
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
E:\编程\C++\自编\001\001.cpp(26) : error C2955: 'Node' : use of class template requires template argument list
E:\编程\C++\自编\001\001.cpp(7) : see declaration of 'Node'
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
E:\编程\C++\自编\001\001.cpp(26) : error C2955: 'Node' : use of class template requires template argument list
E:\编程\C++\自编\001\001.cpp(7) : see declaration of 'Node'
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
E:\编程\C++\自编\001\001.cpp(26) : error C2512: 'Node' : no appropriate default constructor available
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
E:\编程\C++\自编\001\001.cpp(27) : fatal error C1903: unable to recover from previous error(s); stopping compilation
E:\编程\C++\自编\001\001.cpp(63) : while compiling class-template member function '__thiscall LinkList<int>::LinkList<int>(int [],int)'
Error executing cl.exe.

001.obj - 7 error(s), 0 warning(s)

搜索更多相关主题的帖子: 单链 
2007-04-21 17:43
梦自在
Rank: 1
等 级:新手上路
帖 子:110
专家分:0
注 册:2007-3-26
收藏
得分:0 
是这里没高手还是我问的问题太简单了?

http://blog./adreamstar/
2007-04-23 21:53
wood1314
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-4-21
收藏
得分:0 
你的第6行
指针是指向结构体的 要这么声明 : struct Node *next;
在 c or c++ 中 没有 Node 这种类型的数据格式 , 切记.

如果你用这种格式 定义结构体
typedef struct Node{
int x;
struct Node *next;
}Node;
就可以用
Node *直接去定定义一个结构体变量指针.

[此贴子已经被作者于2007-4-25 13:55:43编辑过]

2007-04-25 13:45
jackeyhlj
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:51
专家分:0
注 册:2007-3-27
收藏
得分:0 

第21行你把r要先初始化,然后在取r的地址


2007-04-25 17:55
jackeyhlj
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:51
专家分:0
注 册:2007-3-27
收藏
得分:0 
看错了,你已经把first定义成私有了

2007-04-25 17:58
wood1314
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-4-21
收藏
得分:0 
楼上的都知道为什么它的指针都没有定义成功? 就是因为没有正确声明Node的类型,正解请看3楼
2007-04-25 20:41
fly928sky
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-4-26
收藏
得分:0 

楼上的几个都不对

2007-04-26 21:48
wood1314
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-4-21
收藏
得分:0 
7楼 那你说啊 我说的那里有错 !
我给LZ指出的绝对是他最严重的错误, 其他的我没仔细看,也许会有其他的错误
但是我指出的绝对是最应该改掉的.
2007-04-28 22:05
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
逻辑问题先不说,语法有问题

node的类型不正确,把所有用的node的地方改为node<T>,

因为你定义的是模版类!

3楼说的不对,指针指向结构体可以这样声明,在c++中可以 类名 指针变量;也可以 struct 类名 指针变量;

后者是对c的兼容!

Fight  to win  or  die...
2007-04-29 09:16
wood1314
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-4-21
收藏
得分:0 

是吗? 楼上的! 我不是很知道c++的库函数 ,更别说模版啦 ,没用过啊. 但是那样改不行吗?
c++ 不是兼容c的吗?

2007-04-29 12:55
快速回复:单链表问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018453 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved