bool operator == (const MyString& left, const MyString& right)
{
if(left.value == right.value)
return true;
else return false;
}
bool operator != (const MyString& left, const MyString& right)
{
if(left == right)
return false;
else
return true;
}
bool operator < (const MyString& left, const MyString& right)
{
if(strcmp(left.value, right.value) == -1)
return true;
else
return false;
}
bool operator > (const MyString& left, const MyString& right)
{
if(strcmp(left.value, right.value) == 1)
return true;
else
return false;
}
bool operator <= (const MyString& left, const MyString& right)
{
if(strcmp(left.value, right.value) == -1 || strcmp(left.value, right.value) == 0)
return true;
else
return false;
}
bool operator >= (const MyString& left, const MyString& right)
{
if(strcmp(left.value, right.value) == 1 || strcmp(left.value, right.value) == 0)
return true;
else
return false;
}
这些是我为MyString类实现的比较运算符,但它们失败了我的教授给我的测试程序,可能会使用某些方向。问题是什么?为MyString类重载比较运算符
什么是* *问题? – Puppy 2012-03-28 05:16:35
'strcmp'函数可以返回零,_negative_或_positive_。它不具有“-1”或“1”的特征。 – 2012-03-28 05:33:47
很好的答案,但我不认为'operator =='正在做你想做的事情。你在比较指针,而不是数值。试试这个:'return!strcmp(left.value,right.value);' – parkovski 2012-03-28 05:43:43