Comparing an array of COORDs...

Started by
2 comments, last by Rainniar 20 years ago
Sooo I have an array of COORDs and I want to compare a simple COORD against the entire COORD array. Code looks something like this int x, y; COORD blaharray[] = {(10,5),(10,6),(10,7)}; COORD blah = {x, y}; Then I do something like while(blah != blaharray[scan]) where scan is the array position. c:\MyProgie\tester\tester\tester.cpp(57): error C2678: binary ''!='' : no operator found which takes a left-hand operand of type ''COORD'' (or there is no acceptable conversion) Any thoughts?
Advertisement
You can either compare the X and Y members of the COORD structure directly or create a comparison function and call that instead.

If you insist on using !=, assuming C++, you can supply an overload for operator!=() by doing something like:
bool operator!=(const COORD & lhs, const COORD & rhs) {  return ((lhs.X != rhs.X) || (lhs.Y != rhs.Y));} 
Since your using structs your going to have to make a function or write in the conditional quantity a manual test of the coords variables. For example:
int coordCmpr(COORD crd0, COORD crd1){    if (crd0.x == crd1.x && crd0.y == crd1.y)        return 1;    else         return 0;} 


You gotta do this with all user defined data types, because the compiler doesn''t want to get involved and possibly make an assumption that will cuase the programmer headaches. If coord is a class, you can just overload the != operator I believe, and than when ever that operator appears, a programmer defined function will be called because it handle user defined data types.

I study day and night, memorizing the game.
Thanks a ton guys! My stupid brain is fried and I didn''t even think to realize I couldn''t do a compare on a user defined data type. I''ll just go with the function to do my compare!

Thanks again!

This topic is closed to new replies.

Advertisement