I have this function:

bool Date::testEqual(Date &someDate)
{
if (Date == someDate)

   return true;
      return false;
}

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?

Hi Acidburn,

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

bool Date::testEqual(Date &someDate)
{
if (Date == someDate)

   return true;
else
   return false;
}

:?:

>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?

bool Date::testEqual ( Date& someDate )
{
  return this == &someDate;
}

>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:

int i;
if ( int == i )
  return true;

>but don't you have to write
No, either would work assuming the condition were correct:

if ( condition )
  return true;
else
  return false;
if ( condition )
  return true;
// else
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:

return condition;

umm going back to the question it states, write a function to test the equality of 2 date objects, then gives this code:

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:

if (date1.testEqual(date2))
cout << "the dates are equal" << endl;
else
cout << "the dates are not equal <<endl;

hope this helps to explain why I'm struggling with it

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:

bool Date::testDate ( Date& someDate )
{
  return d == someDate.d && m == someDate.m && y == someDate.y;
}

Or something along those lines depending on the data members of your class.

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

Thanks most appricated

This article has been dead for over six months. Start a new discussion instead.