I have an object which will be used as a key in stl map. Although I had
overridden all the comparison operator (==, >, <, >=, <=), the vc++
compiler still makes complain on:
Compiling...
point.cpp
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) :
error C2678: binary '<' : no operator defined which takes a left-hand
operand of type 'const class Point' (or there is no acceptable
conversion)
C:\Program Files\Microsoft Visual
Studio\VC98\INCLUDE\functional(86) : while compiling class-template
member function 'bool __thiscall std::less<class Point>::operator
()(const class Point &,const class Point &) const'
Error executing cl.exe.
point.exe - 1 error(s), 0 warning(s)
Any suggestion/ advice is very much appreciated.
cheok
#include <map>
#include <assert.h>
using namespace std;
class Point {
public:
Point() {
this->x = 0;
this->y = 0;
}
Point(int x, int y) {
this->x = x;
this->y = y;
}
Point(const Point &p) {
this->x = p.x;
this->y = p.y;
}
Point& operator=(const Point &p) {
this->x = p.x;
this->y = p.y;
return *this;
}
bool operator== (const Point &p) {
return (this->x == p.x && this->y == p.y);
}
bool operator> (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y > p.y);
}
bool operator< (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y < p.y);
}
bool operator<= (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y <= p.y);
}
bool operator>= (const Point &p) {
// For simplicity, we ignore x.
//
return (this->y >= p.y);
}
private:
int x;
int y;
};
int main() {
map<Point, Point> m;
Point p1(100, 100);
Point p2(100, 100);
if(m.find(p1) == m.end()) {
m[p1] = p1;
}
// Although p1 and p2 are different objects,
// their content is the same through comparison on their
// private member variables x and y.
//
// Hence, p2 shouldn't be inserted into the map.
//
if(m.find(p2) == m.end()) {
m[p2] = p2;
assert(0);
}
return 1;
} 4 5895 yc*****@gmail.com wrote: I have an object which will be used as a key in stl map. Although I had overridden all the comparison operator (==, >, <, >=, <=), the vc++ compiler still makes complain on: #include <map> #include <assert.h>
using namespace std;
class Point { public:
bool operator< (const Point &p) { // For simplicity, we ignore x. // return (this->y < p.y);
make operator< a const function
bool operator<(const Point & p) const {
// the code
}
}
int main() { map<Point, Point> m;
Point p1(100, 100); Point p2(100, 100);
if(m.find(p1) == m.end()) { m[p1] = p1;
The stl container map is an ordered collection of pairs, and the
default ordering is done using the function object less(). This
function object defines operator() by taking two const references and
compares them using corresponding operator<. Hence, when you invoke
"find" on m, the less() function eventually calls Point::operator<()
and the arguments that it passes are const references. Hence
Point::operator<() must be declared const.
Hope this helps.
<yc*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... I have an object which will be used as a key in stl map. Although I had overridden all the comparison operator (==, >, <, >=, <=), the vc++ compiler still makes complain on:
Compiling... point.cpp C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class Point' (or there is no acceptable conversion) C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\functional(86) : while compiling class-template member function 'bool __thiscall std::less<class Point>::operator ()(const class Point &,const class Point &) const' Error executing cl.exe.
point.exe - 1 error(s), 0 warning(s)
Any suggestion/ advice is very much appreciated.
cheok
#include <map> #include <assert.h>
using namespace std;
class Point { public:
bool operator< (const Point &p) { // For simplicity, we ignore x. // return (this->y < p.y); }
friend bool operator<(const Point& lhs, const Point& rhs)
{
return lhs.y < rhs.y;
}
-Mike yc*****@gmail.com wrote: I have an object which will be used as a key in stl map. Although I had overridden all the comparison operator (==, >, <, >=, <=), the vc++ compiler still makes complain on:
On a separate note from the suggestions given by the previous replies:
If you're going to use the object as its own key then you should use
std::set rather than std::map. (A set is essentially a map where the
objects are their own keys.)
Mark
On 2005-11-14, yc*****@gmail.com <yc*****@gmail.com> wrote: I have an object which will be used as a key in stl map. Although I had overridden all the comparison operator (==, >, <, >=, <=), the vc++ compiler still makes complain on:
My first suggestion is to use a std::set instead of a map. Why
double your storage requirements?
--
Neil Cerutti This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ian Tompsett |
last post by:
H
I was wondering if it possible for an object to serialize/deserialize itself from XML.
I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
|
by: surrealtrauma |
last post by:
i have a trouble about that:
i want to ask user to enter the employee data (employee no., name, worked
hour, etc.), but i dont know how to sort the data related to a particular
employee as a...
|
by: E. Robert Tisdale |
last post by:
What is an object?
Where did this term come from?
Does it have any relation
to the objects in "object oriented programming"?
|
by: web1110 |
last post by:
Hi again,
I am referring an earlier question here trying to enlarge upon the concept I
am pursuing.:
Each object is a subassembly. They are all significantly different in
detail and content...
|
by: web1110 |
last post by:
If I have 2 variables, A and B, referencing the same object and then do a
A.Dispose(), what happens to B?
|
by: Moe Sizlak |
last post by:
Hi There,
I am trying to write the selected value of a listcontrol when a button is
clicked and I keep getting the error "object not set to a reference of an
object". The libox itself is in a...
|
by: Michael Moreno |
last post by:
Hello,
In a class I have this code:
public object Obj;
If Obj is a COM object I would like to call in the Dispose() method the
following code:
...
|
by: Frederick Gotham |
last post by:
(Before I begin, please don't suggest to me to use "std::vector" rather
than actual arrays.)
I understand that an object can have resources (e.g. dynamically
allocated memory), and so we have to...
|
by: Arpan |
last post by:
The .NET Framework 2.0 documentation states that
An Object variable always holds a pointer to the data, never the data
itself.
Now w.r.t. the following ASP.NET code snippet, can someone please...
|
by: joproulx |
last post by:
Hi,
I was wondering if there was a way with Reflection to find dynamically
if an object was referencing indirectly another object.
A simple example would be:
Object1
|
--Object2
|
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |