挣扎了两个星期之后,线性表的实验失败了,败在一个非常郁闷的地方:调试不过。
不知道发什么神经,之后我又用实验书里面的代码试了一下,坑爹的又是调试不过。书上完整代码如下:
(头文件)
#ifndef LinkList_H
#define LinkList_H
template<class DataType>
struct Node
{
DataType data;
Node<DataType> * next;
};
template<class DataType>
class LinkList
{
public:
LinkList();
LinkList(DataType a[],int n);
~LinkList();
int Locate(DataType x);
void Insert(int i,DataType x);
DataType Delete(int i);
void PrintList();
private:
Node<DataType> * first;
};
#endif
(源文件)
#include<iostream>
using namespace std;
#include"LinkList.h"
template<class DataType>
LinkList<DataType>::LinkList()
{
first=new Node<DataType>;
first->next=NULL;
}
template<class DataType>
LinkList<DataType>::LinkList(DataType a[],int n)
{
Node<DataType> * r,* s;
first=new Node<DataType>;
r=first;
for(int i=0;i<n;i++)
{
s=new Node<DataType>;
s->data=a[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class DataType>
LinkList<DataType>::~LinkList()
{
Node<DataType> * q=NULL;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<class DataType>
void LinkList<DataType>::Insert (int i,DataType x)
{
Node<DataType> * p=first,* s=NULL;
int count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL) throw"location";
else{
s=new NodeV;
s->data=s;
s->next=p->next;p->next=s;
}
}
template<class DataType>
DataType LinkList<DataType>::Delete (int i)
{
Node<DataType> * p=first, * q=NULL;
DataType x;
int count=0;
while(p!=NULL && count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)
throw"location"
else{
q=p->next;x=q->data;
p->next=q->next;
delete q;
return q;
}
}
template<class DataType>
int LinkList<DataType>::Locate (DataType x)
{
Node<DataType> * p=first->next;
int count=i;
while(p!=NULL)
{
if(p->data==x) return count;
p=p->next;
}
return 0;
}
template<class DataType>
void LinkList<DataType>::PrintList ()
{
Node<DataType> * p=first->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
(”main“源文件)
#include<iostream>
using namespace std;
#include"LinkList.cpp"
void main()
{
int r[5]={1,2,3,4,5};
Linklist<int>L(r,5);
cout<<"show the data that are before inserting: "<<endl;
L.PrintList();
try
{
L.Insert(2,3);
}
catch (char * s)
{
cout<<s<<endl;
}
cout<<"apply the order that insert the data and show them: "<<endl;
L.PrintList();
cout<<"the location of the element whose number is 5 directs: ";
cout<<L.Locate(5)<<endl;
cout<<"show the data that are infron the operation of delection: "<<endl;
L.PrintList();
try
{
L.Delect(1);
}
catch(char * s)
{
cout<<s<<end
}
L.PrintList();
}
F:\c++文件\单链表验证实验\LinkList_main.cpp(7) : error C2065: 'Linklist' : undeclared identifier
F:\c++文件\单链表验证实验\LinkList_main.cpp(7) : error C2062: type 'int' unexpected
F:\c++文件\单链表验证实验\LinkList_main.cpp(9) : error C2065: 'L' : undeclared identifier
F:\c++文件\单链表验证实验\LinkList_main.cpp(9) : error C2228: left of '.PrintList' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(12) : error C2228: left of '.Insert' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(19) : error C2228: left of '.PrintList' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(21) : error C2228: left of '.Locate' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(23) : error C2228: left of '.PrintList' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(26) : error C2228: left of '.Delect' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(32) : error C2228: left of '.PrintList' must have class/struct/union type
执行 cl.exe 时出错.
单链表验证实验.exe - 1 error(s), 0 warning(s)
是书上代码不完整么?但事实上它显示的错误是main文件上面的类成员没有定义类型,但是我们都看到在一开始类类型就已经被定义了,并且在main函数中也把类文件引入了(请参看main文件的头文件),这是怎么回事?先前我以为是自己的代码问题,还重写了几次,现在连实验书的代码都出错,而且可以看到,这个错误看起来有些不合理。问题出在什么地方?
顺便说一下,我用的是绿色版,这个跟版本应该无关。