Archived

This topic is now archived and is closed to further replies.

Comparing an array of COORDs...

This topic is 5000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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));
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites