Y
yccheok
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>:perator
()(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;
}
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>:perator
()(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;
}