class, set, < problem

This is a discussion on class, set, < problem within the C++ Programming forums, part of the General Programming Boards category; I have a problem trying to compile some code dealing with multiple class objects, and set containers. Here is a ...

  1. #1
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803

    class, set, < problem

    I have a problem trying to compile some code dealing with multiple class objects, and set containers. Here is a stripped down idea of what I'm trying to get compiled (minus the pieces I think don't contribute to the problem). I'm using MSVC++ 6.0 by the way.

    Polygon.Hpp (defines a polygon class):
    Code:
    class polygon
    {
    public:
        std::string m_strName;  // Name of polygon
        bool operator<(polygon& poly);
    };
    Polygon.Cpp (implementation of polygon class):
    Code:
    #include <string>
    #include "polygon.hpp"
    
    bool polygon::operator<(polygon& poly)
    {
        return m_strName < poly.m_strName;
    }
    main.cpp (definition+implementation of TSV class and test code):
    Code:
    #include <string>
    #include "polygon.hpp"
    #include <set>
    
    class TSV
    {
        std::set<polygon> m_SectorList;
        std::set<polygon> m_SubsectorList;
        // Other member functions follow
        ...
        ...
    };
    
    // Implementation of various TSV member functions follow
    ...
    ...
    
    int main()
    {
        TSV tsv;
    
        // Do stuff with 'tsv' objects member functions
        ...
    
        return 0;
    }
    The error I get while compiling 'main' is as follows:
    error C2678: binary '<' no operator defined which takes a left-hand operand of type 'const class polygon'' (or there is no acceptable conversion) while compiling class-template member function 'bool __thiscall std::less<class_polygon>::operator ()(const class polygon &,const class polygon&) const'

    Now, the error (I believe) happens because less is used as the default sorting criterion for set which uses '<' to compare two items of type polygon against each other. But I thought I took care of this when I implemented the '<' operator in the polygon class. Maybe this is just a brain fart, but do I need to implement this operator in a specific module or in a specific way to get it working properly? Not having all the const's in my version of the '<' operator shouldn't matter should it (that's taken care of by the "acceptable conversion" part I think)? I have tried a few different things but my brain is starting to hurt.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    class polygon
    {
    public:
        std::string m_strName;  // Name of polygon
        bool operator<(const polygon& poly) const; //<-- const
    };

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Ok, it seems to work now but it caused a chain reaction of other problems that required me to add const in several key places. I changed the polygon.hpp file and polygon.cpp file to reference the const's as you described and then tried to compile. This time it said something about there being no copy constructor available even though I did have one written (without const for the argument however). After changing the argument for the copy constructor to use const I recompiled and got another error which required me to change a private member function to use const in its argument. After that it compiled with 0 errors.

    I guess I just need to start reminding myself to use const where it should be used. Now if I can just figure out why I still have that 1 C4786 (:identifier truncated to '255' characters) warning popping up when I have already disabled it by putting #pragma warning(disable:4786) at the top of main.cpp before the #include <set> line I will be happy. Thank you very much!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. problem with const int inside class (c2258)
    By talz13 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 06:34 PM
  5. C++ Programming (Problem One)
    By Nicknameguy in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21