m0_62851145 2024-03-14 23:45 采纳率: 80%
浏览 4

数据结构中遇到的自定义类运算符重载问题

自定义类的运算符重载问题

#include<stdlib.h>//malloc(m),sizeof(x),free(p);
#include<iostream>//cin cout输入输出c++
using namespace std;
//预定义常量根据要解决的问题预定义
#define MAXSIZE 100
#define TRUE 1
#define OK 1
#define ERROR -1
#define INFEASIBLE -1//翻译:不可实行
#define OVERFLOW -2//翻译:溢出
typedef int Status;
typedef struct{
     bool operator ==(const ElemType other)
    {
        if(this->age==other.age&&this->grade==other.grade&&this->name==other.name&&this->student_num==other.student_num)//这里this和operator报错
        {
            return true;
        }
    }
    int student_num;
    char name;
    int age;
    int grade;
    }ElemType;
      bool operator ==(const ElemType other)
    {
        if(this->age==other.age&&this->grade==other.grade&&this->name==other.name&&this->student_num==other.student_num)
        {
            return true;
        }
    }
    //  bool operator ==(const MyStruct other) 
    // {
    //     if (this->a == other.a && this->b == other.b)
    //     {
    //         return true;
    //     }
    //     else
    //     {
    //         return false;
    //     }
    // }
typedef struct{
    ElemType *elem;
    int length;
    }Sqlist;
Sqlist L;
    //int main(){
    //     int *p1=new int;
    //     //从内存里申请一块T类型的内存空间并返回地址
    //     //成功则指针指向新分配的空间,否则返回0(NULL)
    //     //或int *p1=new int(10//     //地址里存了10
    //     delete(p1);//释放p1指向的内存,p1必须是new操作的返回值
    //     int a,b;
    //     cin>>a>>b;//输入
    //     cout<<a<<" "<<b<<endl;//输出
    // }
    Status Initlist_Sq(Sqlist &L){
        L.elem=new ElemType[MAXSIZE];//为顺序表分配空间
        if(!L.elem)exit(OVERFLOW);//存储分配失败
        L.length=0;
        return OK;
    }
    // void DestoryList(Sqlist&L)
    //销毁线性表 
    //{
    //     if(L.elem)delete L.elem;//delete p:释放指针p所指向的内存,p必须是new操作的返回值
    // }
    // void ClearList(Sqlist &L)
    // //清空线性表
    // {
    //     L.length=0;//将线性表的长度设为0// }
    // int GetLength(Sqlist &L)
    // //获取线性表的长度
    // {
    //     return(L.length);
    // }
    //int IsEmpty(Sqlist &L){
    //  //判断线性表是否为空
    // if(L.length==0)return 1;
    // else return 0;
    // }
    int LocatElem(Sqlist L,ElemType e){
 // 顺序表的查找
    for(int i=0;i<L.length;i++)if(L.elem[i].operator==(e))return i+1;//这里报错
    return 0;
    }
   
**__**

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-14 23:45
    关注

    引自免费微信小程序:皆我百晓生

    在C++中,对于自定义类的运算符重载,你需要确保在类内部(成员函数)或外部(友元函数)正确声明并实现该运算符。从你的代码来看,存在几个问题:

    1. ElemType 类中的 == 运算符重载应该作为一个公开成员函数,并且需要一个返回值,即使在条件不满足时也需要返回 false
    struct ElemType {
        // 其他成员变量...
    
        bool operator==(const ElemType& other) const {
            return (this->age == other.age &&
                    this->grade == other.grade &&
                    strcmp(this->name, other.name) == 0 && // 注意:char 类型的名字需要使用 strcmp 比较
                    this->student_num == other.student_num);
        }
    
        // ...
    };
    
    1. 注意,字符数组(如姓名)的比较应使用 strcmp() 函数而非直接使用相等运算符。

    2. LocatElem 函数中调用 operator== 时不需要 .operator==(),只需简单地使用 == 即可。

    修正后的 LocatElem 函数如下:

    int LocatElem(Sqlist L, ElemType e) {
        // 需要注意的是,在遍历过程中应当使用 Sqlist 的引用以避免拷贝
        for (int i = 0; i < L.length; i++) {
            if (L.elem[i] == e) {
                return i + 1;
            }
        }
        return 0;
    }
    

    另外,建议将 #includeusing namespace std; 移到文件顶部,并删除无用的注释和未完成的函数声明。同时,由于你没有提供完整的程序入口(如 main() 函数),所以无法验证整个程序是否可以正常编译和运行。但上述修改应当能解决你在自定义类运算符重载方面遇到的问题。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月14日

悬赏问题

  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响