Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. use object itself as key in stl map

use object itself as key in stl map

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpcssvisual-studiohelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #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; }

    G D 2 Replies Last reply
    0
    • 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::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; }

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • 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::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; }

        D Offline
        D Offline
        Dreamz
        wrote on last edited by
        #3

        yccheok wrote:

        bool operator< (const Point &p) {

        bool operator< (const Point &p) const

        Y 1 Reply Last reply
        0
        • D Dreamz

          yccheok wrote:

          bool operator< (const Point &p) {

          bool operator< (const Point &p) const

          Y Offline
          Y Offline
          yccheok
          wrote on last edited by
          #4

          That's exactly solve my problem, thanks! :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups