Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in defining a map object

Reply
Thread Tools

Problem in defining a map object

 
 
jut_bit_zx@eyou.com
Guest
Posts: n/a
 
      09-14-2005
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:perator == (const tagPOINT& point)
{
if((x == point.x) && (y == point.y))
{
return 1;
}
return 0;
}

bool tagPOINT: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!

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      09-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:perator == (const tagPOINT& point)

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

bool tagPOINT: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.
 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      09-14-2005

(E-Mail 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: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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining iterator type through container object type Urs Thuermann C++ 6 11-04-2011 02:10 PM
Defining object methods in initialize Martin Boese Ruby 3 03-04-2007 07:34 PM
defining class-object declaration in header gives error Ami C++ 3 12-22-2005 12:37 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM



Advertisments