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::operator == (const tagPOINT& point)
{
if((x == point.x) && (y == point.y))
{
return 1;
}
return 0;
}
bool tagPOINT::operator < (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! 2 2225 ju********@eyou.com 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::operator < (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 ju********@eyou.com 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::operator == (const tagPOINT& point)
bool tagPOINT::operator == (const tagPOINT& point) const { if((x == point.x) && (y == point.y)) { return 1; } return 0; }
bool tagPOINT::operator < (const tagPOINT& point)
bool tagPOINT::operator < (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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Harry Pehkonen |
last post by:
I have been defining new class methods when I'm trying to simplify
some code. But I'm thinking I should just define functions within
that method because they aren't useful from the outside anyway....
|
by: John J. Lee |
last post by:
I'm trying define a class to act as a Mock "handler" object for
testing urllib2.OpenerDirector.
OpenerDirector (actually, my copy of it) does dir(handler.__class__)
to find out what methods a...
|
by: Vince Zhang |
last post by:
Hi, all
I wanna make dso that implements OLEDBSimpleProvider interface to interact
mshtml.dll. but meet problem in doing it. Could anybody help me?
I do the following tasks:
1. use VS.NET IDE...
|
by: Howard Jess |
last post by:
All --
I'm trying to solve a problem for which I think the solution will be
to *cheat*; but I don't mind doing so for this case. The background is:
Given an object constructor, and an instance...
|
by: zamir.khan |
last post by:
Hello all,
New to the groups, sorry if this the wrong forum/etiquette. I am coding
a c++ application that requires the use of a timer-triggered event
handler. I decided to use the timer provided...
|
by: Bryce Calhoun |
last post by:
Hello,
First of all, this is a .NET 1.1 component I'm creating.
SUMMARY
-----------------------
This component that I'm creating is, for all intents and purposes, a
document parser (I'm...
|
by: toton |
last post by:
Hi I have a class called Session, which stores a vector of Page, like
vector<PageAlso each Page need's to know the Session to which it is
attached.
Thus I have, (the classes has many other...
|
by: =?Utf-8?B?Z2FkeWE=?= |
last post by:
I use one of 2 arrays dependent on the country.
Rather than say:
if exchangeID = 1 then
dim myPlaceBets() as As UK.exchange.PlaceBets
many statements
myPlaceBetsReq.bets = myPlaceBets
else...
|
by: eBob.com |
last post by:
I have this nasty problem with Shared methods and what I think of as "global
storage" - i.e. storage declared outside of any subroutines or functions.
In the simple example below this "global"...
|
by: Mario Ceresa |
last post by:
Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |