struct stStudent
{
int num;
wstring name;
wstring level;
bool isTwo;
};
自定义的结构体,没有重载operator==,是不能进行等于判断的。
没有重载operator=,可以进行赋值操作
stStudent a, b;
a.isTwo = false;
a.level = L"1";
a.name = L"zhansan";
a.num = 100;
b = a; //正确
if (a == b) //错误
{
cout << "相等" << endl;
}
正确的代码如下:
struct stStudent
{
int num;
wstring name;
wstring level;
bool isTwo;
bool operator==(const stStudent& t)
{
return (num == t.num) && (!isTwo == !t.isTwo) &&
(name == t.name) && (level == t.level);
}
};
stStudent a, b;
a.isTwo = false;
a.level = L"1";
a.name = L"zhansan";
a.num = 100;
b = a; //正确
if (a == b) //正确
{
cout << "相等" << endl;
}
重载赋值运算符
A& operator= (const A& e)
{
...
}