By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

use object itself as key in stl map

P: n/a
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;
}

Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Nov 22 '05 #2

P: n/a

<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
Nov 22 '05 #3

P: n/a
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
Nov 22 '05 #4

P: n/a
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
Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.