title: C++ help with compiler error
body: <p>i'm new to c++ and have been trying to implement the bellman ford algorithm,
  my program doesnt compile i get the following compiler error which is rather cryptic.</p>
  
  <pre><code>D:\ME\MCA\Sem2\LAB\DS\graph_algorithms&gt;g++ bellman_ford.cpp -o bellman.exe
  In file included from bellman_ford.cpp:8:0:
  graph.h: In member function 'void Graph::print_distance_table()':
  graph.h:172:31: error: passing 'const Vertex' as 'this' argument of 'char Vertex::get_name()' discards qualifiers
  </code></pre>
  
  <p>The files that i'm using are graph.h :</p>
  
  <pre><code>#ifndef GRAPH_H
  #define GRAPH_H
  
  class Vertex
  {
    private:
      char vertex_name;
    public:
      Vertex(){};
  
      Vertex(const char n)
      {
        vertex_name = n;
      }
  //Method signatures
      char get_name(); 
  //overloaded operators
      bool operator==(Vertex other) const
      {
        if(vertex_name == other.get_name())
        {
          return true;
        }
        else return false;
      }
  
      bool operator&lt; (Vertex other) const
      {
        if(vertex_name - other.get_name() &lt; 0)
        {
          return true;
        }
        else return false;
      }
  };
  
  class Edge
  {
    private:
      Vertex source,destination;
      int weight;
    public:
      Edge(){};
      Edge(Vertex v1,Vertex v2,int w)
      {
        source = v1;
        destination = v2;
        weight = w;
      }
  
      //Method signatures
      Vertex get_source();
      Vertex get_destn();
      int get_weight();
  };
  
  class Graph
  {
    private:
      list&lt;Vertex&gt; V;
      list&lt;Edge&gt; E;
      map&lt;Vertex,int&gt; distances;
    public:
      Graph(list&lt;Vertex&gt; vertex_list,list&lt;Edge&gt; edge_list)
      {
        V = vertex_list;
        E = edge_list;
      }
  
  //     Method Signatures
       bool add_vertex(Vertex);
       bool remove_vertex(Vertex);
       bool add_edge(Edge);
       bool remove_edge(Edge);
       int total_vertices();
       int total_edges();
       void initialize_distances(Vertex);
       void print_distance_table();
  };
  
  
  /*
   * Methods for the Vertex class are defined first
   */
  
  char Vertex::get_name()
  {
    return vertex_name;
  }
  
  
  /*
   * Methods for the Edge class are defined next
   */
  
  int Edge::get_weight()
  {
    return weight;
  }
  
  Vertex Edge::get_destn()
  {
    return destination;
  }
  
  Vertex Edge::get_source()
  {
    return source;
  }
  
  /*
   * Methods for our Graph class
   */
  
  bool Graph::add_vertex(Vertex u)
  {
    V.push_back(u);
  }
  
  bool Graph::add_edge(Edge e)
  {
    E.push_back(e);
  }
  
  //slightly more tricky will write code when it'll be required i.e. when i implement dfs or some other algo
  //that requires addition and removal of edges and vertices
  bool Graph::remove_vertex(Vertex u)
  {
    //first check if it exists
    //when a vertex is removed then then all the edges that have it as either a source or a destination should also be removed
  }
  
  //
  bool Graph::remove_edge(Edge e)
  {
    //much easier than removing a vertex
    //check if the edge exists and if it does remove it from the list..
  }
  
  int Graph::total_edges()
  {
    return E.size();
  }
  
  int Graph::total_vertices()
  {
    return V.size();
  }
  
  void Graph::initialize_distances(Vertex source)
  {
    distances.clear();
    for(list&lt;Vertex&gt;::iterator it=V.begin(); it != V.end();it++)
    {
      //todo : overload = for the class vertex
      if( *it == source)
      {
        distances[*it] = 0;
      }
      else
      {
        distances[*it] = INT_MAX;
      }
    }
  }
  
  void Graph::print_distance_table()
  {
    map&lt;Vertex,int&gt;::iterator mit;
    for(mit = distances.begin(); mit != distances.end();mit++)
    {
      cout&lt;&lt;mit-&gt;first.get_name()&lt;&lt;"\t"&lt;&lt;mit-&gt;second&lt;&lt;endl; 
    }
  }
  
  #endif //GRAPH_H
  </code></pre>
  
  <p>and bellman_ford.cpp</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;list&gt;
  #include&lt;map&gt;
  #include&lt;climits&gt;
  
  using namespace std;
  
  #include "graph.h"
  
  int main()
  {
    Graph G = Graph(list&lt;Vertex&gt;(), list&lt;Edge&gt;());
    int vertices;
    cout&lt;&lt;"Enter the no. of vertices : ";
    cin&gt;&gt;vertices;
  
    for(int i=0;i&lt;vertices;i++)
    {
      cout&lt;&lt;"Enter the name of the vertex( one character only ) : ";
      char tmp;
      cin&gt;&gt;tmp;
      Vertex tmp_vertex =  Vertex(tmp);
      G.add_vertex(tmp_vertex);
    }
  
    char choice;
    do
    {
      char tmp_src,tmp_destn;
      int tmp_w;
      cout&lt;&lt;"Enter edge( source, destn, weight)";
      cin&gt;&gt;tmp_src&gt;&gt;tmp_destn&gt;&gt;tmp_w;
      G.add_edge( Edge(Vertex(tmp_src),Vertex(tmp_destn),tmp_w) );
  
      cout&lt;&lt;"Add another edge (y|n)? ";
      cin&gt;&gt;choice;
    }while( choice != 'n');
  
    Vertex source_vertex;
    cout&lt;&lt;"\nEnter the source vertex : ";
    cin&gt;&gt;choice;
    source_vertex = Vertex(choice);
    G.initialize_distances(source_vertex);
  
    //now to iterate over the graph and compute the new shortest distances
    G.print_distance_table();
  
  
    return 0;
  }
  </code></pre>
  
  <p>i'm not very familiar with the stl map which seems to be the source of the error in line 172 of the graph.h file.
  if i remove the reference to mit->first.get_name() then it works but i wish to print the vertex names too.
  What is the mistake that i'm making that gives this error?</p>