丢弃限定符错误,为什么我不能从非const方法调用非const方法?

时间:2016-01-28 21:23:02

标签: c++

我正在实现一个无向图,但我遇到了一些问题:

error: passing 'const std::__cxx11::list<Graph<char>::Edge, std::allocator<Graph<char>::Edge> >' as 'this' argument discards qualifiers [-fpermissive]

课程是这样的:

template<class T> class Graph {
    class Vertex;                   //Dichiarazione della classe Vertex, definizione in seguito..
    class Edge;                     //Dichiarazione della classe Edge, definizione in seguito..

    typedef typename set<Graph<T>::Vertex>::iterator VertexIt;
    typedef typename list<Graph<T>::Edge>::iterator EdgeIt;

    class Vertex{
        friend bool operator<(const Vertex& x,const Vertex& y) { return x.data<y.data; } // returns x<y
    public:
        Vertex(T data) : data(data) {}
        void printVertex() const;
        void printAdj() const;

        T data;
        list<Graph<T>::Edge> adj;
    };

    class Edge{
    public:
        Edge(VertexIt link, int weight) : link(link), weight(weight){}
        void printEdge() const;
        VertexIt link;
        int weight;
    };



private:
    set<Graph<T>::Vertex> graph;


public:
    /**COSTRUTTORE E DISTRUTTORE*/
    Graph();
    virtual ~Graph();
    /**GETTER & SETTER*/

    /**METODI DI CLASSE*/
    void addVertex(T data);
    void printGraph() const;
    void link(T a, T b, int weight);
    void link(VertexIt a, VertexIt b, int weight);
};

#endif /* GRAPH_H_ */

错误来了,当我尝试push_back()时,也将函数标记为const错误仍然存​​在。为什么我不能用非const方法调用非const方法?

template <class T> void Graph<T>::link(VertexIt a, VertexIt b, int weight) {
    if(a == graph.end() or b == graph.end())
        return;
    (a->adj).push_back(Edge(b,weight));
    (b->adj).push_back(Edge(a,weight));

}

0 个答案:

没有答案