use object itself as key in stl map
-
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::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 #include 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 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; }
-
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::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 #include 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 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; }
I think your definition of map m is incorrect for a start... shouldnt it be :- map < Point, Point > m; ?? - ie you havnt told the compiler what you're storing in map m. One thing you have done (great :-) ) is define the < operator for your class .. from Cprogramming.com :- "To use the map class, you will need to include and maps are part of the std namespace. Maps require two, and possibly three, types for the template: std::map < key_type, data_type, [comparison_function] > Notice that the comparison function is in brackets, indicating that it is optional so long as your key_type has the less-than operator, < , defined -- so you don't need to specify a comparision function for so-called primitive types such as int, char, or even for the string class. Moreover, if you overloaded the < operator for your own class, this won't be necessary. The reason that the key type needs the less-than operator is that the keys will be stored in sorted order -- this means that if you want to retrieve every key, value pair stored in the map, you can retrieve them in the order of the keys. " http://www.cprogramming.com/tutorial/stl/stlmap.html[^] [Edit] I think you also need to define the < operator with two operands thus :- bool operator <(const Point &LHS, const Point &RHS){ return LHS.y < RHS.y; } [/Edit] 'g' -- modified at 4:49 Monday 14th November, 2005
-
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::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 #include 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 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; }