2013-03-31 2 views
0

이것은 정말 간단 할 수도 있지만 해결하지 못한 것 같습니다. 내 Vertex 내에서 std::list<Edge> 가지고 있지만 push_front 같은 메서드를 호출 할 때 listconst 말하는 오류가 발생하고 그것을 밀어 넣을 수 없습니다. 어떤 이유에서 컴파일러가 std::list<Edge>const std::list<Edge>으로 변환하고 있다고 생각합니다. 내 코드가 잘 설정되어 있지는 않지만 단지 숙제 일 뿐이므로 몇 가지 단축키를 사용하고 있습니다.C++ 컴파일러가 목록을 const리스트로 변환합니다.

헤더 파일 : g++ -c Graph.cpp 실행

void Graph::add_edge(unsigned int from, unsigned int to, unsigned int weight) 
{ 
Vertex find_vert; 
find_vert.id = from; 
set<Vertex>::iterator from_v = _vertices.find(find_vert); 
Edge new_edge; 
new_edge.to = to; 
new_edge.weight = weight; 

from_v->edges.push_front(new_edge); // ERROR HERE 
} 

컴파일러 오류 메시지 : 오류의 원인

#ifndef GRAPH_H 
#define GRAPH_H 

#include <set> 
#include <list> 

class Edge{ 
public: 
    unsigned int to; 
    unsigned int weight; 
}; 

class Vertex{ 
public: 
    unsigned int id; 
    std::list<Edge> edges; 

    bool operator<(const Vertex& other) const{ 
     return id < other.id; 
    } 
}; 

class Graph{ 

public: 
    void add_vertex(unsigned int id); 
    void add_edge(unsigned int from, unsigned int to, unsigned int weight); 
    std::set<Vertex> get_vertices(); 
    std::list<Edge> get_edges(unsigned int id); 

private: 
    std::set<Vertex> _vertices; 
    unsigned int size = 0; 


}; 

라인 std::set

Graph.cpp:23:38: error: passing ‘const std::list<Edge>’ as ‘this’ argument of ‘void std::list<_Tp, 
_Alloc>::push_front(const value_type&) [with _Tp = Edge; _Alloc = std::allocator<Edge>; std::list<_Tp, 
_Alloc>::value_type = Edge]’ discards qualifiers [-fpermissive] 
+3

아마도'const' 한정자가있는 함수에서 그 행을 실행할 것입니다. – James

+0

'from_v'는 무엇입니까? – 0x499602D2

+0

'from_v'은' :: iterator from_v = _vertices.find (find_vert) '로 설정됩니다. – seanwatson

답변

4

내용이 있기 때문에, 암시 적 const 있습니다 내용 변경 d 정렬 순서를 무효화합니다.

이렇게하면 이 암시 적으로 const이됩니다.

set<Vertex>::iterator from_v = _vertices.find(find_vert); 

그리고 당신의 오류는 당신이 const 개체를 수정하기 위해 노력하고 있음을 말하고있다.

from_v->edges.push_front(new_edge); 
// ^^^^^^ const ^^^^^^^^^^ non-const behavior 
+0

아, 훨씬 더 의미가 있습니다. 감사! – seanwatson

관련 문제