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

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


#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 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了