这段代码错哪了???
碰到一个关于在一个线性排序集合中搜索结点的例子,老是编译不过去,好像是构造函数有问题,大家帮忙看看!!!
//DataList.h
#ifndef DATALIST_H
#define DATALIST_H
#include <iostream.h>
template <class Type> class dataList;
template <class Type> class Node{//数据表中结点类的定义
friend class dataList<Type>;
public:
Node(const Type &value):key(value){}//构造函数
Type getKey()const{return key;}//读取关键码
void setKey(Type k){key=k;}//修改关键码
private:
Type key;//关键码域
};
template <class Type> class dataList{//数据表类定义
public:
dataList(int sz=10):ArraySize(sz),Element(new Node <Type>[sz]){}
virtual ~dataList(){delete [] Element;}
friend ostream &operator<<(ostream &OutStream,const dataList<Type> &OutList);
friend istream &operator>>(istream &InStream,dataList<Type> &InList);
protected:
Type *Element;//数据表中存储数据的数组
int ArraySize,CurrentSize;//数据最大长度和当前长度
};
template <class Type> class searchList:public dataList<Type>{
//搜索表searchList继承了dataList,并且增加了成员函数Search()
public:
searchList(int sz=10):dataList<Type>(sz){}
virtual ~searchList(){}
virtual int Search(const Type& x)const;
};
template <class Type> ostream& operator<<(ostream& OutStream,const dataList<Type>& OutList){
OutStream<<"Array Contents:\n";//输出表的所有表项到OutStream
for(int i=0;i<OutList.CurrentSize;i++)OutStream<<OutList.Element[i]<<' ';
OutStream<<endl;//输出表的当前长度到OutStream
OutStream<<"Array Current Size:"<<OutList.CurrentSize<<endl;
return OutStream;
}
template <class Type> istream& operator>>(istream& InStream,dataList<Type> & InList){
cout<<"Enter Array Current Size:";
Instream>>InList.CurrentSize;//从Instream输出表的当前长度
cout<<"Enter array elements:\n";
for(int i=0;i<InList.CurrentSize;i++)//从Instream输出表的当前表项
{cout<<"Element"<<i<<":";InStream>>InList.Element[i];}
return InStream;
}
template <class Type> int searchList<Type>::Search(const Type& x)const{
//在搜索表searchList中顺序搜索其关键码为x的数据对象,要求数据对象在表中从下标1开始存放,
//第0号位置作为控制搜索过程自动结束的"监视哨"使用。若找到则函数返回对象在表中的位置
//i,否则返回0
Element[0].setKey(x);int i=CurrentSize;//将x送0号位置设置监视哨
while(Element[i].getKey()!=x)i--;//从后向前顺序搜索
return i;
}
#endif
//main.cpp
#include "DataList.h"
const int Size=10,Value=0;
void main(){
searchList<float> List1(Size);//定义搜索表List1
float Target;int Location;
cin>>List1;cout<<List1;//输入List1,注意0号位置
cout<<"Search for a float:";cin>>Target;//输入要搜索的数据
if((Location=List1.Search(Target))!=0)//搜索到的位置为location
cout<<"Found at index:"<<Location<<endl;//搜索成功,输出找到的位置
else
cout<<"Not found.\n";//搜索不成功,返回失败信息
}
//VC6中的调试信息:
main.cpp
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(20) : error C2512: 'Node<float>' : no appropriate default constructor available
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(20) : while compiling class-template member function '__thiscall dataList<float>::dataList<float>(int)'
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(20) : fatal error C1903: unable to recover from previous error(s); stopping compilation
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(20) : while compiling class-template member function '__thiscall dataList<float>::dataList<float>(int)'
Error executing cl.exe.
main.obj - 2 error(s), 0 warning(s)