2016-03-26 23 views
1

std::set ile kullanılan özel bir sınıf oluşturmaya çalışıyorum. Bunun için özel bir karşılaştırıcı sunmam gerektiğini biliyorum, bu yüzden operator<'u aşırı yükledim. Ben aşağıdaki hatayı alıyorum kod set<Edge> a; set<Edge> b = a;, ile set kopyalamaya çalıştığınızda Ama: karşılaştırma operatörü const işaretlenmesi gerekir olarakStd set kopya ataması ile özel sınıf nasıl oluşturulur?

/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; 
    } 
}; 

cevap

4

bool operator<(const Edge& other) const 

olun. std::set'daki anahtarlar const,const örneğinde çağrılır, dolayısıyla const işaretli olmalıdır.

İlgili konular