-
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.
-
Code:
class polygon
{
public:
std::string m_strName; // Name of polygon
bool operator<(const polygon& poly) const; //<-- const
};
-
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!