#include<iostream>
#include<conio.h>
#include<string.h>
#define OK 1
#define MAXSIZE 100
using namespace std;
typedef int Status;
typedef struct
{
char name[5];
float price;
}Book;
typedef Book ElemType;
typedef struct
{
ElemType *elem;
int length;
}SqList;
Status InitList(SqList &L)
{
//L.elem=new ElemType[MAXSIZE];
L.elem=new ElemType[MAXSIZE];
if(!L.elem)
exit(-1);
L.length=0;
return OK;
}
Status GetList(SqList L,int i,ElemType &book)
{
if(i<1||i>L.length)
exit(-1);
else
book=L.elem[i-1];
return OK;
}
int LocateElem(SqList L,ElemType e)
{
int i;
for( i=0;i<L.length;i++)
if(L.elem[i]==e)
return i+1;
return 0;
}
int main()
{
SqList L;
InitList(L);
L.elem[0].price=0;
L.length++;
L.elem[1].price=1;
L.length++;
ElemType ceshi;
GetList(L,2,ceshi);
LocateElem(L,ceshi);
cout<<ceshi.price;
getch();
return 0;
}
这是我写的代码,编译后报错
--------------------Configuration: 线性表 - Win32 Debug--------------------
Compiling...
线性表.cpp
E:\数据结构\线性表.cpp(41) : error C2784: 'bool __cdecl std::operator ==(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocator<_Ty> &' from 'Book'
E:\数据结构\线性表.cpp(41) : error C2784: 'bool __cdecl std::operator ==(const class std::istreambuf_iterator<_E,_Tr> &,const class std::istreambuf_iterator<_E,_Tr> &)' : could not deduce template argument for 'const class std::istreambuf_iterator<_
E,_Tr> &' from 'Book'
E:\数据结构\线性表.cpp(41) : error C2784: 'bool __cdecl std::operator ==(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for 'const class std::rev
erse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'Book'
E:\数据结构\线性表.cpp(41) : error C2784: 'bool __cdecl std::operator ==(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair<_T1,_T2> &' from 'Book'
E:\数据结构\线性表.cpp(41) : error C2676: binary '==' : 'Book' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.
线性表.exe - 5 error(s), 0 warning(s)
经过测验,是查找线性表函数LocateList中的if(L.elem[i]==e)发生错误,也就是结构体匹配出现了问题,请问完成结构体匹配,代码该如何修改呢?