c++ - How to create custom class used with std set copy assignment? -


i try create custom class used std::set. know need provide custom comparator overloaded operator<. when try copy set code set<edge> a; set<edge> b = a;, following error:

/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/include/c++/v1/__functional_base:63:21: invalid operands binary expression ('const edge' , 'const edge')

class edge { public:     edge(int v, int w, double weight):v(v),w(w),weight(weight){}     int other(int vertex){ return v ? w : vertex == w;}     int v,w;     double weight;     friend std::ostream& operator<<(std::ostream& out, const edge& e)     {         out<<e.v<<' '<<e.w<<' '<<"weight:"<<e.weight<<'\n';         return out;     }     bool operator<(const edge& other)     {         return weight < other.weight;     } }; 

make

bool operator<(const edge& other) const 

as comparison operator must marked const. keys in std::set const, operator< invoked on const instance, hence must marked const.


Comments