CPoint class [was: pls help]


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: CPoint class [was: pls help]

  1. #1
    Join Date
    Jul 2006
    Posts
    93

    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)

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    -------------------------------

  3. #3
    Join Date
    Jul 2006
    Posts
    93

    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

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    -------------------------------

  5. #5
    Join Date
    Jul 2006
    Posts
    93

    thks for quick reply

    i will try and and i update the astatus very soon..

  6. #6
    Join Date
    Jul 2006
    Posts
    93

    again error:< has too many parameters..

    i kept whatever said
    but it is showing < has too many parameters....

  7. #7
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    -------------------------------

  8. #8
    Join Date
    Jul 2006
    Posts
    93
    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 ..

  9. #9
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    -------------------------------

  10. #10
    Join Date
    Jul 2006
    Posts
    93
    hey thank you very much i cleared all ther problems drkyb..................
    cheers....

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  4. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  5. Replies: 205
    Last Post: 09-26-2001, 02: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center