自定义类的运算符重载问题
#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;
}
**__**