how to test equality of 2 objects

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

how to test equality of 2 objects

 
0
  #1
Mar 31st, 2005
I have this function:

  1. bool Date::testEqual(Date &someDate)
  2. {
  3. if (Date == someDate)
  4.  
  5. return true;
  6. return false;
  7. }

but it returns 3 errors ,
C:\computer science programming\excerise sheet 5\2 - Date.cpp(91) : error C2143: syntax error : missing ')' before '=='
C:\computer science programming\excerise sheet 5\2 - Date.cpp(91) : error C2143: syntax error : missing ';' before '=='
C:\computer science programming\excerise sheet 5\2 - Date.cpp(91) : error C2059: syntax error : ')'
I dont see whats wrong? surely objects are treat the same as char, ints etc?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: how to test equality of 2 objects

 
0
  #2
Mar 31st, 2005
Hi Acidburn,

Don't know if it's correct, but don't you have to write:

  1. bool Date::testEqual(Date &someDate)
  2. {
  3. if (Date == someDate)
  4.  
  5. return true;
  6. else
  7. return false;
  8. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to test equality of 2 objects

 
0
  #3
Mar 31st, 2005
>if (Date == someDate)
Date is a type, someDate is an object of that type. A comparison between them is nonsensical. I assume you want to test the parameter to see if it's the same object as the method is being called on?
  1. bool Date::testEqual ( Date& someDate )
  2. {
  3. return this == &someDate;
  4. }
>surely objects are treat the same as char, ints etc?
They can be made to be, but you still wouldn't do this even with built in types:
  1. int i;
  2. if ( int == i )
  3. return true;
>but don't you have to write
No, either would work assuming the condition were correct:
  1. if ( condition )
  2. return true;
  3. else
  4. return false;
  1. if ( condition )
  2. return true;
  3. // else
  4. return false;
As long as the code after your conditional will never be executed if the condition is true, the whole construct is equivalent to an if..else statement.

However, if you're just returning or evaluating a boolean value, it's simpler to just use the condition itself rather than a conditional statement:
  1. return condition;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: how to test equality of 2 objects

 
0
  #4
Mar 31st, 2005
umm going back to the question it states, write a function to test the equality of 2 date objects, then gives this code:
  1. bool Date:: testEqual(Date & someDate)

thats all it gave ...leaving us to write the function body so I tried.

The function is called in main by:
  1.  
  2. if (date1.testEqual(date2))
  3. cout << "the dates are equal" << endl;
  4. else
  5. cout << "the dates are not equal <<endl;
  6.  

hope this helps to explain why I'm struggling with it
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to test equality of 2 objects

 
0
  #5
Mar 31st, 2005
There are two measures of equality. Value equality says that two objects (that may or may not be the same object) have the same value. Address equality says that two objects are in fact, the same object because they both reside at the same address in memory.

Most likely you want to test for value equality:
  1. bool Date::testDate ( Date& someDate )
  2. {
  3. return d == someDate.d && m == someDate.m && y == someDate.y;
  4. }
Or something along those lines depending on the data members of your class.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: how to test equality of 2 objects

 
0
  #6
Mar 31st, 2005
amazing thats fixed it :o, cheers dude , means I can now get on with my operator overloading functions

Thanks most appricated
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC