| | |
how to test equality of 2 objects
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
I have this function:
but it returns 3 errors ,
I dont see whats wrong? surely objects are treat the same as char, ints etc?
C++ Syntax (Toggle Plain Text)
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 : ')'
Hi Acidburn,
Don't know if it's correct, but don't you have to write:
Don't know if it's correct, but don't you have to write:
C++ Syntax (Toggle Plain Text)
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?
>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:
>but don't you have to write
No, either would work assuming the condition were correct:
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:
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)
bool Date::testEqual ( Date& someDate ) { return this == &someDate; }
They can be made to be, but you still wouldn't do this even with built in types:
C++ Syntax (Toggle Plain Text)
int i; if ( int == i ) return true;
No, either would work assuming the condition were correct:
C++ Syntax (Toggle Plain Text)
if ( condition ) return true; else return false;
C++ Syntax (Toggle Plain Text)
if ( condition ) return true; // else return false;
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)
return condition;
I'm here to prove you wrong.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
umm going back to the question it states, write a function to test the equality of 2 date objects, then gives this code:
thats all it gave ...leaving us to write the function body so I tried.
The function is called in main by:
hope this helps to explain why I'm struggling with it
C++ Syntax (Toggle Plain Text)
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)
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:
Or something along those lines depending on the data members of your class.
Most likely you want to test for value equality:
C++ Syntax (Toggle Plain Text)
bool Date::testDate ( Date& someDate ) { return d == someDate.d && m == someDate.m && y == someDate.y; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Simple C++ program terminate prematurely (C++)
- .h/.C ? Test pgm ... what goes where? (C++)
- quicktime VR object loading problem (Graphics and Multimedia)
Other Threads in the C++ Forum
- Previous Thread: at the moment this is a stack of queue, i am trying to make it into first in first ou
- Next Thread: Instantiating class objects??
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







