返回信息流☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 19:57:32 2008) 提到:
#include<iostream>
using namespace std;
template<class T>
class LinkList
{
public:
LinkList(T a[],int n);
~LinkList();
void Get(int i);
private:
Node<T> *first;
};
Linklist::Linklist(T a[],int n)
{first=new Node<T>;
first->next=NULL;
for(i=0;i<n;i++)
{
s=new Node<T>;s->data=a[i];
s->next=first->next;
first->next=s;
}
}
void Linklist::Get(int i)
{
p=first->next;j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p)throw"位置";
else cout<<p->data<<endl;
}
int main()
{
T a[]={1,2,3,4};
Linklist(a,4);
Get(2);
}
只是想试试书上的算法,结果一堆错误 能帮我看看是怎么回事么?谢谢啦
E:\C++程序\通讯录\2.cpp(11) : error C2143: syntax error : missing ';' before '<'
E:\C++程序\通讯录\2.cpp(12) : see reference to class template instantiation 'LinkList<T>' being compiled
E:\C++程序\通讯录\2.cpp(11) : error C2501: 'Node' : missing storage-class or type specifiers
E:\C++程序\通讯录\2.cpp(12) : see reference to class template instantiation 'LinkList<T>' being compiled
E:\C++程序\通讯录\2.cpp(11) : error C2059: syntax error : '<'
E:\C++程序\通讯录\2.cpp(12) : see reference to class template instantiation 'LinkList<T>' being compiled
E:\C++程序\通讯录\2.cpp(11) : error C2238: unexpected token(s) preceding ';'
E:\C++程序\通讯录\2.cpp(12) : see reference to class template instantiation 'LinkList<T>' being compiled
E:\C++程序\通讯录\2.cpp(13) : error C2653: 'Linklist' : is not a class or namespace name
E:\C++程序\通讯录\2.cpp(13) : error C2065: 'T' : undeclared identifier
E:\C++程序\通讯录\2.cpp(13) : error C2146: syntax error : missing ')' before identifier 'a'
E:\C++程序\通讯录\2.cpp(13) : error C2501: 'Linklist' : missing storage-class or type specifiers
E:\C++程序\通讯录\2.cpp(13) : error C2059: syntax error : ')'
E:\C++程序\通讯录\2.cpp(14) : error C2143: syntax error : missing ';' before '{'
E:\C++程序\通讯录\2.cpp(14) : error C2447: missing function header (old-style formal list?)
E:\C++程序\通讯录\2.cpp(23) : error C2653: 'Linklist' : is not a class or namespace name
E:\C++程序\通讯录\2.cpp(25) : error C2065: 'p' : undeclared identifier
E:\C++程序\通讯录\2.cpp(25) : error C2065: 'first' : undeclared identifier
E:\C++程序\通讯录\2.cpp(25) : error C2227: left of '->next' must point to class/struct/union
E:\C++程序\通讯录\2.cpp(25) : error C2065: 'j' : undeclared identifier
E:\C++程序\通讯录\2.cpp(28) : error C2227: left of '->next' must point to class/struct/union
E:\C++程序\通讯录\2.cpp(32) : error C2227: left of '->data' must point to class/struct/union
E:\C++程序\通讯录\2.cpp(37) : error C2146: syntax error : missing ';' before identifier 'a'
E:\C++程序\通讯录\2.cpp(37) : error C2065: 'a' : undeclared identifier
E:\C++程序\通讯录\2.cpp(37) : error C2059: syntax error : ']'
E:\C++程序\通讯录\2.cpp(37) : error C2143: syntax error : missing ';' before '{'
E:\C++程序\通讯录\2.cpp(37) : error C2143: syntax error : missing ';' before '}'
E:\C++程序\通讯录\2.cpp(40) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.
☆─────────────────────────────────────☆
guo (计忆邮心|郭) 于 (Tue Nov 4 20:02:18 2008) 提到:
Node是?
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 20:23:28 2008) 提到:
【 在 guo 的大作中提到: 】
: Node是?
结点
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 20:40:19 2008) 提到:
知道是结点,
1楼的意思,是问你,Node的代码呢?
你都没有Node的定义,直接上来就用了,当然编译器就不认识了。
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 20:57:25 2008) 提到:
【 在 ericyosho 的大作中提到: 】
: 知道是结点,
: 1楼的意思,是问你,Node的代码呢?
: 你都没有Node的定义,直接上来就用了,当然编译器就不认识了。
我定义了Node以后还是有错误啊
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 20:59:40 2008) 提到:
【 在 wing070186 的大作中提到: 】
: #include<iostream>
: using namespace std;
: template<class T>
: ...................
我加了Node的定义
struct Node
{T data;
Node<T> *next;};
还是有一堆错误啊
☆─────────────────────────────────────☆
prating (竹下生|久夜|亲亲尾巴爱尾巴) 于 (Tue Nov 4 21:06:18 2008) 提到:
i j a 等变量都是天上掉下来的么。。。
都已经int main了,return呢?
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 21:06:39 2008) 提到:
这个代码本来就是一堆错误
我随便问你几个:
1. class名字叫LinkedList,但是你的构造函数是Linkedlist,大小写的L就不一样
2. 构造函数里面的变量 i 在哪里声明了?
3. Get函数里面的变量 p 和 j 又在哪里声明了?
4. main函数里的LinkedList (a, 4)是什么意思?对象名呢?
5. 最后调用Get时候的对象名呢?
6. 你的代码光有new,没有delete的么?析构函数也没写啊。
随便看看就那么多错误,这个是什么书啊?
☆─────────────────────────────────────☆
purevirtual (天之健|杨无敌|hf) 于 (Tue Nov 4 21:07:39 2008) 提到:
盗版书里的一段伪代码
【 在 ericyosho (ericyosho) 的大作中提到: 】
: 这个代码本来就是一堆错误
: 我随便问你几个:
: 1. class名字叫LinkedList,但是你的构造函数是Linkedlist,大小写的L就不一样
: ...................
☆─────────────────────────────────────☆
prating (竹下生|久夜|亲亲尾巴爱尾巴) 于 (Tue Nov 4 21:09:47 2008) 提到:
【 在 ericyosho 的大作中提到: 】
: 这个代码本来就是一堆错误
: 我随便问你几个:
: 1. class名字叫LinkedList,但是你的构造函数是Linkedlist,大小写的L就不一样
: ...................
re...
而且。。下面的错误信息不是写的很清楚哪里错了么。。。LZ不会看么。。。
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 21:09:51 2008) 提到:
=。=
这伪代码,伪的还真真~~
【 在 purevirtual 的大作中提到: 】
: 盗版书里的一段伪代码
☆─────────────────────────────────────☆
prating (竹下生|久夜|亲亲尾巴爱尾巴) 于 (Tue Nov 4 21:10:54 2008) 提到:
【 在 purevirtual 的大作中提到: 】
: 盗版书里的一段伪代码
orz。。比真代码还难读的伪代码。。。
☆─────────────────────────────────────☆
purevirtual (天之健|杨无敌|hf) 于 (Tue Nov 4 21:11:53 2008) 提到:
其实世上本没有伪代码,写的人多了也就有了
【 在 ericyosho (ericyosho) 的大作中提到: 】
: =。=
: 这伪代码,伪的还真真~~
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 21:12:39 2008) 提到:
【 在 wing070186 的大作中提到: 】
: #include<iostream>
: using namespace std;
: template<class T>
: ...................
#include<iostream>
using namespace std;
template<class T>
struct Node
{T data;
Node<T> *next;};
class LinkList
{
public:
LinkList (T a[],int n);
~LinkList();
void Get(int i);
private:
Node<T> *first;
};
Linklist::Linklist(T a[],int n)
{first=new Node<T>;
first->next=NULL;
for(int i=0;i<n;i++)
{
s=new Node<T>;s->data=a[i];
s->next=first->next;
first->next=s;
}
}
void Linklist::Get(int i)
{Node<T> *p;
int j;
p=first->next;j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p)throw"位置";
else cout<<p->data<<endl;
}
int main()
{
T a[]={1,2,3,4};
Linklist A(a,4);
A.Get(2);
return 0;}
改完了还是很多错误啊
E:\C++程序\通讯录\2.cpp(11) : error C2629: unexpected 'class LinkList ('
E:\C++程序\通讯录\2.cpp(11) : error C2238: unexpected token(s) preceding ';'
E:\C++程序\通讯录\2.cpp(15) : error C2065: 'T' : undeclared identifier
E:\C++程序\通讯录\2.cpp(17) : error C2653: 'Linklist' : is not a class or namespace name
E:\C++程序\通讯录\2.cpp(17) : error C2146: syntax error : missing ')' before identifier 'a'
E:\C++程序\通讯录\2.cpp(17) : error C2501: 'Linklist' : missing storage-class or type specifiers
E:\C++程序\通讯录\2.cpp(17) : error C2059: syntax error : ')'
E:\C++程序\通讯录\2.cpp(18) : error C2143: syntax error : missing ';' before '{'
E:\C++程序\通讯录\2.cpp(18) : error C2447: missing function header (old-style formal list?)
E:\C++程序\通讯录\2.cpp(27) : error C2653: 'Linklist' : is not a class or namespace name
E:\C++程序\通讯录\2.cpp(30) : error C2065: 'first' : undeclared identifier
E:\C++程序\通讯录\2.cpp(30) : error C2227: left of '->next' must point to class/struct/union
E:\C++程序\通讯录\2.cpp(33) : error C2440: '=' : cannot convert from 'struct Node<T> *' to 'struct Node *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\C++程序\通讯录\2.cpp(37) : error C2593: 'operator <<' is ambiguous
E:\C++程序\通讯录\2.cpp(42) : error C2146: syntax error : missing ';' before identifier 'a'
E:\C++程序\通讯录\2.cpp(42) : error C2065: 'a' : undeclared identifier
E:\C++程序\通讯录\2.cpp(42) : error C2059: syntax error : ']'
E:\C++程序\通讯录\2.cpp(42) : error C2143: syntax error : missing ';' before '{'
E:\C++程序\通讯录\2.cpp(42) : error C2143: syntax error : missing ';' before '}'
E:\C++程序\通讯录\2.cpp(43) : error C2146: syntax error : missing ';' before identifier 'A'
E:\C++程序\通讯录\2.cpp(43) : error C2065: 'A' : undeclared identifier
E:\C++程序\通讯录\2.cpp(44) : error C2228: left of '.Get' must have class/struct/union type
执行 cl.exe 时出错.
2.obj - 1 error(s), 0 warning(s)
为什么会有像第一条的那种错误呢?
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 21:19:50 2008) 提到:
因为你的LinkedList是一个模板类,里面含有未定义的类型T,
但是你在声明的时候,没有加template <typename T>
所以就有了第一行这样的错误。
☆─────────────────────────────────────☆
purevirtual (天之健|杨无敌|hf) 于 (Tue Nov 4 21:21:54 2008) 提到:
re
然后你在写函数外部定义的时候也要加上template<typename T>才行,当然偷懒的话直接把定义写在class里面
【 在 ericyosho (ericyosho) 的大作中提到: 】
: 因为你的LinkedList是一个模板类,里面含有未定义的类型T,
: 但是你在声明的时候,没有加template <typename T>
: 所以就有了第一行这样的错误。
: ...................
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 21:33:11 2008) 提到:
【 在 ericyosho 的大作中提到: 】
: 因为你的LinkedList是一个模板类,里面含有未定义的类型T,
: 但是你在声明的时候,没有加template <typename T>
: 所以就有了第一行这样的错
不太明白哦 应该加在哪里呢 谢谢咯
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 21:34:01 2008) 提到:
加在class LinkedList这一行的前面。
还有你在class外面定义构造函数和Get的时候
上面也要加template<typename T>
而且也都要写成 LinkedList<T>::LinkedList 和 LinkedList<T>::Get
【 在 wing070186 的大作中提到: 】
: 不太明白哦 应该加在哪里呢 谢谢咯
☆─────────────────────────────────────☆
prating (竹下生|久夜|亲亲尾巴爱尾巴) 于 (Tue Nov 4 21:36:21 2008) 提到:
突然发现这楼变矮了。。。
话说LZ后面的类名什么的还是不对。。
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 21:53:56 2008) 提到:
【 在 ericyosho 的大作中提到: 】
: 加在class LinkedList这一行的前面。
: 还有你在class外面定义构造函数和Get的时候
: 上面也要加template<typename T>
: ...................
#include<iostream>
using namespace std;
template<class T>
struct Node
{T data;
Node<T> *next;};
template<class T>
class LinkList
{
public:
LinkList (T a[],int n);
~LinkList();
void Get(int i);
private:
Node<T> *first;
};
template<class T>
LinkList::LinkList<T>(T a[],int n)
{first=new Node<T>;
first->next=NULL;
for(int i=0;i<n;i++)
{
s=new Node<T>;s->data=a[i];
s->next=first->next;
first->next=s;
}
}
template<class T>
void LinkList<T>::Get(int i)
{Node<T> *p;
int j;
p=first->next;j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p)throw"位置";
else cout<<p->data<<endl;
}
int main()
{
T a[]={1,2,3,4};
LinkList A(a,4);
A.Get(2);
return 0;}
我改了 但是还是不对啊E:\C++程序\43\43.cpp(19) : error C2955: 'LinkList' : use of class template requires template argument list
E:\C++程序\43\43.cpp(17) : see declaration of 'LinkList'
E:\C++程序\43\43.cpp(19) : error C2039: 'LinkList<T>' : is not a member of 'LinkList'
E:\C++程序\43\43.cpp(17) : see declaration of 'LinkList'
E:\C++程序\43\43.cpp(19) : error C2935: 'LinkList<T>' : template-class-id redefined as a global function
E:\C++程序\43\43.cpp(45) : error C2065: 'T' : undeclared identifier
E:\C++程序\43\43.cpp(45) : error C2146: syntax error : missing ';' before identifier 'a'
E:\C++程序\43\43.cpp(45) : error C2065: 'a' : undeclared identifier
E:\C++程序\43\43.cpp(45) : error C2059: syntax error : ']'
E:\C++程序\43\43.cpp(45) : error C2143: syntax error : missing ';' before '{'
E:\C++程序\43\43.cpp(45) : error C2143: syntax error : missing ';' before '}'
E:\C++程序\43\43.cpp(46) : error C2955: 'LinkList' : use of class template requires template argument list
E:\C++程序\43\43.cpp(17) : see declaration of 'LinkList'
E:\C++程序\43\43.cpp(46) : error C2514: 'LinkList' : class has no constructors
E:\C++程序\43\43.cpp(17) : see declaration of 'LinkList'
E:\C++程序\43\43.cpp(46) : error C2262: 'A' : cannot be destroyed
E:\C++程序\43\43.cpp(47) : error C2662: 'Get' : cannot convert 'this' pointer from 'class LinkList' to 'class LinkList<T> &'
Reason: cannot convert from 'class LinkList' to 'class LinkList<T>'
Conversion requires a second user-defined-conversion operator or constructor
执行 cl.exe 时出错.
43.obj - 1 error(s), 0 warning(s)
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 21:59:10 2008) 提到:
是LinkList<T>::LinkList,你写错了。
还有很多错误呢,
1. main函数里面的a前面,怎么可以用T呢?你得明确告诉它,你这个数组a是个什么类型的
2. 你的析构函数呢?光看到你各种new了,内存泄漏得很厉害
☆─────────────────────────────────────☆
wing070186 (wing) 于 (Tue Nov 4 22:02:17 2008) 提到:
【 在 ericyosho 的大作中提到: 】
: 是LinkList<T>::LinkList,你写错了。
: 还有很多错误呢,
: 1. main函数里面的a前面,怎么可以用T呢?你得明确告诉它,你这个数组a是个什么类型的
: ...................
数组a 不是T型的么?
☆─────────────────────────────────────☆
Xer (SL小分队|小x|benogy) 于 (Tue Nov 4 22:04:33 2008) 提到:
建议还是先看看有关章节再写
【 在 wing070186 (wing) 的大作中提到: 】
: 数组a 不是T型的么?
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 22:05:44 2008) 提到:
T只是一个符号,告诉编译器,
"我定义这些类的这些个位置,有可能是任意类型的。到运行的时候,我会告诉你们T到底是什么。"
结果你在运行的时候,还是没有告诉它,自然就错了。
☆─────────────────────────────────────☆
ericyosho (ericyosho) 于 (Tue Nov 4 22:18:29 2008) 提到:
这个贴很不错啊,让我复习了一下对于模板的使用。
总结一下,然后小X合了吧。
1. 在声明模板类的时候,必须加上 template <typename T>子句
2. 定义模板类的各个成员函数时,也必须加上 template <typename T>子句,并且在写类限定符是要加上<T>
3. 永远要对称,构造函数里new出来的,析构函数里就要delete掉。当然可以用其他的智能指针代替,这是另外一个话题了
(@@)~~~~~
☆─────────────────────────────────────☆
purevirtual (天之健|杨无敌|hf) 于 (Tue Nov 4 22:36:10 2008) 提到:
赞 这个贴子应该合了加精
【 在 ericyosho (ericyosho) 的大作中提到: 】
: 这个贴很不错啊,让我复习了一下对于模板的使用。
: 总结一下,然后小X合了吧。
: 1. 在声明模板类的时候,必须加上 template <typename T>子句
: ...................
☆─────────────────────────────────────☆
jokerlee (Jackal The Dire) 于 (Wed Nov 5 08:15:03 2008) 提到:
。。。。编译器的输出都说得很清楚了
☆─────────────────────────────────────☆
ys912 (nereus) 于 (Wed Nov 5 09:41:07 2008) 提到:
可以运行了
#include<iostream>
using namespace std;
template<class T> struct Node
{
T data;
Node<T> *next;
};
template<class T> class LinkList
{
public:
LinkList(T a[],int n);
//~LinkList();
void Get(int i);
private:
Node<T> *first;
};
template<class T>LinkList<T>::LinkList(T a[],int n)
{
first=new Node<T>;
first->next=NULL;
for(int i=0;i<n;i++)
{
Node<T> *s=new Node<T>;
s->data=a[i];
s->next=first->next;
first->next=s;
}
}
template <class T>void LinkList<T>::Get(int i)
{
Node<T> *p=first->next;
int j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p)throw"位置";
else cout<<p->data<<endl;
}
int main()
{
int a[]={1,2,3,4};
LinkList<int> obj(a,4);
obj.Get(3);
return 0;
}
☆─────────────────────────────────────☆
xiaoliu1985 (waiting...) 于 (Wed Nov 5 10:53:55 2008) 提到:
要学会根据提示自己调试程序啊
这是一条镜像帖。来源:北邮人论坛 / cpp / #15472同步于 2008/11/5
CPP机器人发帖
[合集] [求助]链表的c++算法,请大家帮帮我
Xer
2008/11/5镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。