Problem in defining a map object

J

jut_bit_zx

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!
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

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.
 
M

msalters

(e-mail address removed) 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top