C++ 帮助解决编译器错误

标签 c++ compiler-errors

我是 c++ 的新手,一直在尝试实现 bellman ford 算法, 我的程序没有编译我得到以下编译器错误,这是相当神秘的。

D:\ME\MCA\Sem2\LAB\DS\graph_algorithms>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

我使用的文件是 graph.h :

#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< (Vertex other) const
    {
      if(vertex_name - other.get_name() < 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<Vertex> V;
    list<Edge> E;
    map<Vertex,int> distances;
  public:
    Graph(list<Vertex> vertex_list,list<Edge> 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<Vertex>::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<Vertex,int>::iterator mit;
  for(mit = distances.begin(); mit != distances.end();mit++)
  {
    cout<<mit->first.get_name()<<"\t"<<mit->second<<endl; 
  }
}

#endif //GRAPH_H

和 bellman_ford.cpp

#include<iostream>
#include<list>
#include<map>
#include<climits>

using namespace std;

#include "graph.h"

int main()
{
  Graph G = Graph(list<Vertex>(), list<Edge>());
  int vertices;
  cout<<"Enter the no. of vertices : ";
  cin>>vertices;

  for(int i=0;i<vertices;i++)
  {
    cout<<"Enter the name of the vertex( one character only ) : ";
    char tmp;
    cin>>tmp;
    Vertex tmp_vertex =  Vertex(tmp);
    G.add_vertex(tmp_vertex);
  }

  char choice;
  do
  {
    char tmp_src,tmp_destn;
    int tmp_w;
    cout<<"Enter edge( source, destn, weight)";
    cin>>tmp_src>>tmp_destn>>tmp_w;
    G.add_edge( Edge(Vertex(tmp_src),Vertex(tmp_destn),tmp_w) );

    cout<<"Add another edge (y|n)? ";
    cin>>choice;
  }while( choice != 'n');

  Vertex source_vertex;
  cout<<"\nEnter the source vertex : ";
  cin>>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;
}

我不太熟悉 STL 映射,它似乎是 graph.h 文件第 172 行错误的来源。 如果我删除对 mit->first.get_name() 的引用,那么它可以工作,但我也希望打印顶点名称。 我犯了什么错误导致了这个错误?

最佳答案

std::mapfirst(“key”)元素是const,因为它们不能被改变。您不能在 const 对象上调用非 const 成员函数,因此会出现错误。

解决方案是将Vertex::get_name声明为const:char get_name() const;这是一个告诉编译器 get_name() 不会改变对象的契约。

总的来说,这是一个很好的习惯。您应该始终将非变异成员函数声明为 const,因为它允许编译器发现错误。

关于C++ 帮助解决编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5522228/

相关文章:

c++ - 在没有 double 类型的 C 编译器上解析 double IEEE 浮点

指向类成员的模板函数的C++指针

C 编译错误 : array type has incomplete element type

c++ - 为 ARM 平台编译库时出错

c++ - 在 STL 算法中使用模板谓词的问题

c++ - 将 time_point 降低到给定持续时间的推荐方法

c++ - 是否可以对基类非 QObject 类的虚函数进行槽化

c++ - 返回 const 模板类型的模板函数

C++ vector 错误(Visual C++ 2008 Express Edition)

python - TypeError : “NoneType” object is not iterable