|
查看: 1113|回复: 11
|
compare object?(c++)
[复制链接]
|
|
|
我想问问,如果我现在有一个class,里面有一个function叫equals,是用来compare两个object的,
也就是accept一个新object来和当前的object compare,如果object相同就return true,else return false。
但是我尝试在class里面写一个function用==来compare会有error,
bool equals(Item newItem){
if(newItem==this)
return true;
else
return false;
}
虽然那个this我不知道行不行得通,但是error是和operator有关,
1>c:\users\menghui\desktop\c++\assignment 1\assignment 1\Item.h(61) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Item' (or there is no acceptable conversion)
有没有人可以帮帮我,我在网上查到的都是用pointer的,如果不用pointer做得到吗?谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 6-7-2009 12:20 AM
|
显示全部楼层
回复 1# 千年刹 的帖子
你要如何compare ?
compare 同一个 object, 是同一个 address? 还是 object 的某些value 相同? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-7-2009 12:33 AM
|
显示全部楼层
回复 2# onlylonly 的帖子
相同value~~~~~~~~~
但是是object里的全部value
[ 本帖最后由 千年刹 于 6-7-2009 12:40 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-7-2009 12:35 AM
|
显示全部楼层
我找到的相关资料,是写多一个叫 bool operator==(); 的东西,但是我看不太明,还没到那个程度 |
|
|
|
|
|
|
|
|
|
|
发表于 6-7-2009 10:04 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 6-7-2009 08:23 PM
|
显示全部楼层
原帖由 千年刹 于 5-7-2009 11:09 PM 发表 
我想问问,如果我现在有一个class,里面有一个function叫equals,是用来compare两个object的,
也就是accept一个新object来和当前的object compare,如果object相同就return true,else return false。
但 ...
建议你打好基础先,搞清楚什么是object (value), pointer, reference....
this是pointer,你拿它来跟object比较当然会出现那个violation....
comparator RHS最好用reference,省去检查是否null (不然dereference member 时access violation...).- bool operator==(const T& rhs) const
- {
- if (&rhs == this)
- {
- return true;
- }
- else if (this->value == rhs.value)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
复制代码
[ 本帖最后由 yeenfei 于 6-7-2009 08:32 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 6-7-2009 10:02 PM
|
显示全部楼层
回复 3# 千年刹 的帖子
value 的话, 那你怎么那this pointer 来comapre ?
虽然你的syntax 也写错了。
你要得是
bool equals(const Item &newItem) const
{
if(&newItem==this) // true if newitem and ur object is same object ,i,e they have same address
return true;
else if( newItem.getValueA() == getValueA() && newItem.getValueB() == getValueB() && ...................... etc .................. )
return true;
else
return false;
}
[ 本帖最后由 onlylonly 于 6-7-2009 10:03 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-7-2009 10:19 PM
|
显示全部楼层
回复 5# geekman 的帖子
谢谢你 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-7-2009 10:30 PM
|
显示全部楼层
回复 6# yeenfei 的帖子
不好意思刚接触oop,很多东西都不会,
那个this我只是想test下,不过我尝试过换去accept 2 object arguments然后compare,还是有一样的error,就是那个operator==的问题,
然后找到一些类似以上的方法,但是还没消化到
还在研究着,无论如何,谢谢~ |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-7-2009 10:34 PM
|
显示全部楼层
回复 7# onlylonly 的帖子
因为我看到网上说this代表着当前的object,所以我就试试这样弄,原来不可以的
谢谢你的code,这个最好消化,但是有一点我不明白,为什么我查到的code在bracket后面都有加const,这代表着什么? |
|
|
|
|
|
|
|
|
|
|
发表于 6-7-2009 11:54 PM
|
显示全部楼层
回复 10# 千年刹 的帖子
你是说 void something() const ?
这个是代表这个 function 不会 modify 任何的 field, 所以declare const ( const member function ).
这样的华, const object 久可以用这个 function 了。 因为 const object 只能由用 const function。
google 以下 c++ const function 会有很多很好的资料 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-7-2009 10:15 PM
|
显示全部楼层
回复 11# onlylonly 的帖子
ok 我明白了。。谢谢你 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|