Problem in defining a map object

Discussion in 'C++' started by jut_bit_zx@eyou.com, Sep 14, 2005.

  1. Guest

    Consider the following code:

    #include <iostream>
    #include <map>
    using namespace std;

    typedef struct tagPOINT
    {
    tagPOINT();
    bool operator == (const tagPOINT& point);
    bool operator < (const tagPOINT& point);
    unsigned int x;
    unsigned int y;
    } POINT;

    class less_point
    {
    public:
    bool operator()(const tagPOINT& point1,const tagPOINT& point2)
    {
    return (point1.y < point2.y);
    }
    };

    tagPOINT::tagPOINT()
    {
    x = 0;
    y = 0;
    }

    bool tagPOINT::eek:perator == (const tagPOINT& point)
    {
    if((x == point.x) && (y == point.y))
    {
    return 1;
    }
    return 0;
    }

    bool tagPOINT::eek:perator < (const tagPOINT& point)
    {
    return (y < point.y);
    }

    typedef map<POINT, int>::value_type pixelValue;
    typedef map<POINT, int, less_point> pointmap;

    int main(int argc, char* argv[])
    {
    pointmap pixelArray;
    POINT point;
    pixelArray.insert(pixelValue(point, 1));
    cout<<pixelArray[point]<<endl;
    return 0;
    }

    Q: The compile environment is Microsoft VC6.0, and the compile erros
    are:"cannot convert 'this' pointer from 'const class less_point' to
    'class less_point &' Conversion loses qualifiers" . what's wrong with
    the code?

    Thanks!
     
    , Sep 14, 2005
    #1
    1. Advertising

  2. wrote:
    > Consider the following code:
    >
    > #include <iostream>
    > #include <map>
    > using namespace std;
    >
    > typedef struct tagPOINT
    > {
    > tagPOINT();
    > bool operator == (const tagPOINT& point);
    > bool operator < (const tagPOINT& point);

    bool operator == (const tagPOINT& point) const;
    bool operator < (const tagPOINT& point) const;
    > unsigned int x;
    > unsigned int y;
    > } POINT;
    >
    > class less_point
    > {
    > public:
    > bool operator()(const tagPOINT& point1,const tagPOINT& point2)

    bool operator()(const tagPOINT& point1,const tagPOINT& point2) const
    > {
    > return (point1.y < point2.y);
    > }
    > };
    >
    > tagPOINT::tagPOINT()
    > {
    > x = 0;
    > y = 0;
    > }
    >
    > bool tagPOINT::eek:perator == (const tagPOINT& point)

    bool tagPOINT::eek:perator == (const tagPOINT& point) const
    > {
    > if((x == point.x) && (y == point.y))
    > {
    > return 1;
    > }
    > return 0;
    > }
    >
    > bool tagPOINT::eek:perator < (const tagPOINT& point)

    bool tagPOINT::eek:perator < (const tagPOINT& point) const
    > {
    > return (y < point.y);
    > }
    >
    > typedef map<POINT, int>::value_type pixelValue;
    > typedef map<POINT, int, less_point> pointmap;
    >
    > int main(int argc, char* argv[])
    > {
    > pointmap pixelArray;
    > POINT point;
    > pixelArray.insert(pixelValue(point, 1));
    > cout<<pixelArray[point]<<endl;
    > return 0;
    > }
    >
    > Q: The compile environment is Microsoft VC6.0, and the compile erros
    > are:"cannot convert 'this' pointer from 'const class less_point' to
    > 'class less_point &' Conversion loses qualifiers" . what's wrong with
    > the code?
    >
    > Thanks!
    >


    'const' is your friend...

    /S.
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Sep 14, 2005
    #2
    1. Advertising

  3. msalters Guest

    schreef:

    > Consider the following code:
    >
    > #include <iostream>
    > #include <map>
    > using namespace std;
    >
    > typedef struct tagPOINT
    > {
    > tagPOINT();
    > bool operator == (const tagPOINT& point);
    > bool operator < (const tagPOINT& point);
    > unsigned int x;
    > unsigned int y;
    > } POINT;


    That's C. In C++, you just write

    struct Point {
    /// ...
    Uppercase is for macro's, and you don't need typedefs on structs
    anymore.

    > class less_point
    > {
    > public:
    > bool operator()(const tagPOINT& point1,const tagPOINT& point2)
    > {
    > return (point1.y < point2.y);
    > }
    > };


    You probably want points to be unequal if point1.y==point2.y but
    point1.x!=point2.x. This less_point considers them equal.

    > bool tagPOINT::eek:perator < (const tagPOINT& point)
    > {
    > return (y < point.y);
    > }


    You probably want to add a second const here. After all, this compares
    two points. Read the FAQ to find out where the second const goes.

    HTH,
    Michiel Salters
     
    msalters, Sep 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    446
    Peter Koch Larsen
    Jul 2, 2004
  2. Ami
    Replies:
    3
    Views:
    319
    Marc BAUDY
    Dec 22, 2005
  3. Urs Thuermann
    Replies:
    6
    Views:
    514
    Urs Thuermann
    Nov 4, 2011
  4. Martin Boese

    Defining object methods in initialize

    Martin Boese, Mar 4, 2007, in forum: Ruby
    Replies:
    3
    Views:
    120
    Robert Klemme
    Mar 4, 2007
  5. Sonny Chee
    Replies:
    4
    Views:
    86
    F. Senault
    Jan 17, 2009
Loading...

Share This Page