-
CPoint class [was: pls help]
hai to all
once i posted this problem..
again i am getting this problem
pls help me..
i have to use cpoint class in map datastructure in vc++
but it is trowing an error
err:binary < nooperator defined which takes left hand side operand of type
class cpoint
some body tole me that just ovverride < operator in cpoint class
but cpoint class is system class
where i have to ovveride...pls reply..
i willbe waiting for your responce
or any other alternative like
can i convert map into vector (is it possible)
-
Hi,
your operator < should be a global function unless you want to derive your own CMyPoint - class from the CPoint - class which might be possible depending on whether CPoint has a virtual destructor.
Make sure that your operator can distinguish all CPoints that are different, though.
Code:
bool operator < (const CPoint& p1, const CPoint& p2)
{
// don't: return (p1.x+p1.y) < (p2.x+p2.y); because (1,2) != (2,1) but
// (1,2) < (2,1) returns false!
// and
// (2,1) < (1,2) returns false!
return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y); // lexicographic <
}
Unless you artificially make CPoints totally ordered you won't be able to add all points into the map.
Cheers,
D
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
where i have to put this code..
thks for quick reply
but i dont know where to put this code
can i put in cpoint class
-
Hi,
No, you can't put it in the CPoint class, unless the CPoint class is one of your own classes.
if you only use the operator in one file, then just put the implementation in that file before you use your map.
If you intend to use the operator in other files too, put a declaration of the operator in a header-file and the implentation in a cpp file. Don't forget to link then to your project and dont forget to include the header in every file where you use your map.
Does that answer the question?
D
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
thks for quick reply
i will try and and i update the astatus very soon..
-
again error:< has too many parameters..
i kept whatever said
but it is showing < has too many parameters....
-
Hi,
I just did a quick test and it compiles perfectly fine. The only thing I could think of is, that you made the operator a class member, in which case you have to modify the operator to
Code:
bool operator < (const CPoint& p)
{
// don't: return (p1.x+p1.y) < (p2.x+p2.y); because (1,2) != (2,1) but
// (1,2) < (2,1) returns false!
// and
// (2,1) < (1,2) returns false!
return x < x || (x == p.x && y < p.y); // lexicographic <
}
In case of a member operator you have the implicit "this" argument, which means you need only the right hand side in the parameter list.
If that's not it, then send the code and the error message it produces.
Cheers,
D
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
i have tried like you..
but in that caes
it is ahowing
x is not a menmber of class
and also for y..
i tell you the my problem scenario..
in my project all cpoint pairs are stored in a map..
from that there are accessing an ddispalying that points on a screen ..
-
Hi,
just make your operator global and use the first version I gave you. x and y are members of the CPoint class and if your class does not derive from it and if you do not define x and y yourself in your class you won't be able to access them.
Cheers,
D
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
hey thank you very much i cleared all ther problems drkyb..................
cheers....
Similar Threads
-
By Osiris43 in forum .NET
Replies: 1
Last Post: 08-04-2006, 12:15 PM
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
Replies: 205
Last Post: 09-26-2001, 01:37 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks