944,045 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5837
  • C++ RSS
Mar 31st, 2005
0

how to test equality of 2 objects

Expand Post »
I have this function:

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

but it returns 3 errors ,
Quote ...
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?
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 31st, 2005
0

Re: how to test equality of 2 objects

Hi Acidburn,

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

C++ Syntax (Toggle Plain Text)
  1. bool Date::testEqual(Date &someDate)
  2. {
  3. if (Date == someDate)
  4.  
  5. return true;
  6. else
  7. return false;
  8. }
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 31st, 2005
0

Re: how to test equality of 2 objects

>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?
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. if ( condition )
  2. return true;
  3. else
  4. return false;
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. return condition;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 31st, 2005
0

Re: how to test equality of 2 objects

umm going back to the question it states, write a function to test the equality of 2 date objects, then gives this code:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Mar 31st, 2005
0

Re: how to test equality of 2 objects

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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 31st, 2005
0

Re: how to test equality of 2 objects

amazing thats fixed it :o, cheers dude , means I can now get on with my operator overloading functions

Thanks most appricated
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: at the moment this is a stack of queue, i am trying to make it into first in first ou
Next Thread in C++ Forum Timeline: Instantiating class objects??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC