이것은 정말 간단 할 수도 있지만 해결하지 못한 것 같습니다. 내 Vertex
내에서 std::list<Edge>
가지고 있지만 push_front
같은 메서드를 호출 할 때 list
const
말하는 오류가 발생하고 그것을 밀어 넣을 수 없습니다. 어떤 이유에서 컴파일러가 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]
아마도'const' 한정자가있는 함수에서 그 행을 실행할 것입니다. – James
'from_v'는 무엇입니까? – 0x499602D2
'from_v'은' :: iterator from_v = _vertices.find (find_vert) '로 설정됩니다. –
seanwatson