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):
Polygon.Cpp (implementation of polygon class):Code:class polygon { public: std::string m_strName; // Name of polygon bool operator<(polygon& poly); };
main.cpp (definition+implementation of TSV class and test code):Code:#include <string> #include "polygon.hpp" bool polygon::operator<(polygon& poly) { return m_strName < poly.m_strName; }
The error I get while compiling 'main' is as follows: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; }
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.