qq_43412960 2019-12-19 17:07 采纳率: 78.6%
浏览 611
已结题

这是一个关于数据结构的结构体相等问题


#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)发生错误,也就是结构体匹配出现了问题,请问完成结构体匹配,代码该如何修改呢?

  • 写回答

1条回答 默认 最新

  • bacmive 2019-12-19 19:50
    关注

    需要给结构体类型Elemtype增加一个“ ==操作符重载函数",如下面代码中的bool operator ==(const ElemType& a, const ElemType& b)函数

    #include<iostream>
    #include<conio.h>
    #include<string.h>
    #include<cstdlib>
    #define OK 1
    #define MAXSIZE 100
    using namespace std;
    typedef  int Status;
    typedef  struct
    {
        char name[5];
        float price;
    }Book;
    
    typedef Book ElemType;
    
    bool operator ==(const ElemType& a, const ElemType& b)
    {
        for(int i=0; i<5;i++)
            if(a.name[i]!=b.name[i]) return false;
        if(a.price != b.price) return false;
        return true;
    }
    
    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=5;
        L.length++;
        ElemType ceshi;
        GetList(L,2,ceshi);
        LocateElem(L,ceshi);
        cout<<ceshi.price;
        getch();
        return 0;
    }
    

    参考资料:
    https://www.cnblogs.com/lyttt/p/11774402.html
    https://blog.csdn.net/sunny1996/article/details/51242184/

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀