2016-03-26 10 views
1

std::setで使用されるカスタムクラスを作成しようとしました。私はoperator<をオーバーロードしたので、カスタムコンパレータを用意する必要があることを知っています。私は 、コードset<Edge> a; set<Edge> b = a;でセットをコピーしようとすると、しかし、私は次のエラーを取得:std set copy assignmentで使用されるカスタムクラスを作成するには?

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__functional_base:63:21: Invalid operands to binary expression ('const Edge' and '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; 
    } 
}; 

答えて

4

を比較演算子はconstをマークする必要がありますよう

bool operator<(const Edge& other) const 

してください。 std::setのキーはconstであるため、constインスタンスでoperator<が呼び出されるため、constとマークする必要があります。

関連する問題