这段代码错哪了???

MashiMaro2002 2003-03-04 01:02:45
碰到一个关于在一个线性排序集合中搜索结点的例子,老是编译不过去,好像是构造函数有问题,大家帮忙看看!!!
//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)
...全文
84 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wshcdr 2003-03-08
  • 打赏
  • 举报
回复
gz
Frank001 2003-03-08
  • 打赏
  • 举报
回复
……
is >> IN.key; //改为is >> IN;
return is;
}

template <class Type> ostream& operator<<(ostream& os,Node<Type> & ON)
{
os << ON.key; //改为os << ON;
return os;
……

不过,这样虽然可以编译通过,但我觉得你的程序里还是存在问题,没仔细看,你自己在看看吧。
MashiMaro2002 2003-03-08
  • 打赏
  • 举报
回复
修改了一下,在
template <class Type> istream& operator>>(istream& InStream,dataList<Type> & InList)中加了个const变成
template <class Type> istream& operator>>(istream& InStream,const dataList<Type> & InList)
仍然还有一个错,怎么办????大家关注一下了!!!
ompiling...
main.cpp
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(50) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
G:\My Works\Programs\Visual C++\数据结构(用面向对象方法与C++描述)\SearchList\main.cpp(7) : see reference to function template instantiation 'class istream &__cdecl operator >>(class istream &,const class dataList<float> &)' being compiled
Error executing cl.exe.
MashiMaro2002 2003-03-05
  • 打赏
  • 举报
回复
to:prf8(风筝)
怎么还是有错,主要是Node类中重载的输入输出操作符不能访问Node类中的private对象Key
Compiling...
main.cpp
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(68) : error C2248: 'key' : cannot access protected member declared in class 'Node<float>'
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(18) : see declaration of 'key'
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(53) : see reference to function template instantiation 'class istream &__cdecl operator >>(class istream &,class Node<float> &)' being compiled
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(74) : error C2248: 'key' : cannot access protected member declared in class 'Node<float>'
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(18) : see declaration of 'key'
g:\my works\programs\visual c++\数据结构(用面向对象方法与c++描述)\searchlist\datalist.h(42) : see reference to function template instantiation 'class ostream &__cdecl operator <<(class ostream &,class Node<float> &)' being compiled
Error executing cl.exe.

main.obj - 2 error(s), 0 warning(s)
prf8 2003-03-04
  • 打赏
  • 举报
回复
主要是没有重载NOde的>>,<<;
prf8 2003-03-04
  • 打赏
  • 举报
回复
#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(){};
Node(const Type &value):key(value){}//构造函数
Type getKey()const{return key;}//读取关键码
void setKey(Type k){key=k;}//修改关键码
friend ostream &operator<<(ostream &OutStream,const Node<Type> &ON);
friend istream &operator>>(istream &InStream,const Node<Type> &IN);
private:
Type key;//关键码域
};
template <class Type> istream& operator>>(istream& is,Node<Type> & IN)
{
is >> IN.key;
return is;
}

template <class Type> ostream& operator<<(ostream& os,Node<Type> & ON)
{
os << ON.key;
return os;
}



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:
Node<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
MashiMaro2002 2003-03-04
  • 打赏
  • 举报
回复
很遗憾,修改后对程序的错误没有丝毫影响!!!
yuanhen 2003-03-04
  • 打赏
  • 举报
回复
Type *Element 改成:Node<Type> *Element;

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧