Linear table second experiment ended in failure

After struggling for two weeks, the linear form of the experiment failed, defeated by a very depressing place: Debugging however.

I do not know what nerve, then I experiment with the code inside the book tried it, pit father is debugging however. Book complete code is as follows:

(head File)

#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

(Source File)

#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" source file)

#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();	
}


Debug errors as follows:


F: \ c ++ file \ single chain verification experiment \ LinkList_main.cpp (. 7): error C2065: 'Linklist': undeclared identifier
F: \ c ++ file \ single chain verification experiment \ LinkList_main.cpp (7): error C2062 : type ' int 'Unexpected
F.: \ C ++ files \ single chain verification experiment \ LinkList_main.cpp (. 9): error C2065:' L ': undeclared identifier
F.: \ C ++ files \ single chain verification experiment \ LinkList_main.cpp (9): error C2228 : left of '.PrintList' MUST have have class / struct / Union type
F.: \ C ++ files \ single chain verification experiment \ LinkList_main.cpp (12): error C2228 : left of '.Insert' must have class / struct / union type
F: \ c ++ file \ single chain verification experiment \ LinkList_main.cpp (. 19): error C2228: left of '.PrintList' MUST have have class / struct / Union type
F: \ c ++ file \ single chain verification experiment \ LinkList_main.cpp ( 21): error C2228: left of '.Locate' must have class / struct / union type
F: \ c ++ file \ single chain verification experiment \ LinkList_main.cpp (23 is): error C2228: left of '.PrintList' MUST have have class / struct / Union type
F: \ c ++ file \ single chain verification experiment \ LinkList_main.cpp ( 26 is): error C2228: left of '.Delect' MUST have have class / struct / Union type
F.: \ C ++ files \ single chain verification experiment \ LinkList_main.cpp (32): error C2228 : left of '.PrintList' must have class / struct union type /
error executing cl.exe.

single chain verification experiment .exe - 1 error (s), 0 warning (s)


The code book is not complete it? But in fact it shows the error class members above the main file type is not defined, but we have seen at the beginning of class type has been defined, and also introduced the class files in the main function (see main file header files), how is this going? Earlier, I thought it was his code issues, but also to rewrite a few times, and now even test the code book are wrong, and you can see, this error may seem unreasonable. The problem is in place?

By the way, I use the green version, this version should be nothing to do with.

Guess you like

Origin blog.csdn.net/z1094219402/article/details/40559877