C++ C++;关于编译器错误的帮助

C++ C++;关于编译器错误的帮助,c++,compiler-errors,C++,Compiler Errors,我使用的文件是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

我使用的文件是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
#ifndef图
#定义图
类顶点
{
私人:
字符名称;
公众:
顶点(){};
顶点(常量字符n)
{
顶点名称=n;
}
//方法签名
char get_name();
//重载运算符
布尔运算符==(顶点其他)常量
{
if(vertex_name==other.get_name())
{
返回true;
}
否则返回false;
}
布尔运算符<(顶点其他)常量
{
if(顶点名称-其他.获取名称()<0)
{
返回true;
}
否则返回false;
}
};
阶级边缘
{
私人:
顶点源、目标;
整数权重;
公众:
边(){};
边(顶点v1,顶点v2,整数w)
{
来源=v1;
目的地=v2;
重量=w;
}
//方法签名
顶点获取_源();
顶点获取_destn();
int get_weight();
};
类图
{
私人:
清单五;
清单E;
地图距离;
公众:
图(列表顶点列表、列表边列表)
{
V=顶点列表;
E=边缘列表;
}
//方法签名
布尔加_顶点(顶点);
bool移除_顶点(顶点);
布尔加_边(边);
bool去除_边(边);
int total_顶点();
int总_边();
无效初始化距离(顶点);
无效打印距离表();
};
/*
*首先定义顶点类的方法
*/
char Vertex::get_name()
{
返回顶点名称;
}
/*
*接下来定义Edge类的方法
*/
int-Edge::get_-weight()
{
返回重量;
}
顶点边::get_destn()
{
返回目的地;
}
顶点边::获取_源()
{
返回源;
}
/*
*图类的方法
*/
布尔图::添加_顶点(顶点u)
{
V.推回(u);
}
布尔图::添加_边(边e)
{
E.推回(E);
}
//在需要的时候,也就是说,当我实现dfs或其他算法时,编写代码要稍微复杂一些
//这需要添加和删除边和顶点
布尔图::删除顶点(顶点u)
{
//首先检查它是否存在
//删除顶点后,将其作为源或目标的所有边也应删除
}
//
布尔图::删除_边(边e)
{
//比删除顶点容易得多
//检查该边是否存在,以及是否将其从列表中删除。。
}
int图::总边数()
{
返回E.size();
}
int图::总顶点数()
{
返回V.size();
}
空图::初始化距离(顶点源)
{
距离。清除();
for(list::iterator it=V.begin();it!=V.end();it++)
{
//todo:重载=对于类顶点
如果(*it==源)
{
距离[*it]=0;
}
其他的
{
距离[*it]=INT_MAX;
}
}
}
空图::打印距离表()
{
迭代器;
for(mit=distance.begin();mit!=distance.end();mit++)
{
coutstd::map
第一个
(“键”)元素是
常量
,因为它们可能不会被更改。不能在
常量
对象上调用非
常量
成员函数,因此会出现错误

解决方案是将
Vertex::get_name
声明为
const
char get_name()const
;这是一个协定,它告诉编译器
get_name()
不会更改对象

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

第一个
(“键”)
std::map
的元素是
const
,因为它们可能不会被更改。不能在
const
对象上调用非
const
成员函数,因此会出现错误

解决方案是将
Vertex::get_name
声明为
const
char get_name()const
;这是一个协定,它告诉编译器
get_name()
不会更改对象


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

您必须使用“
const
”限定符声明方法:

const char* get_name() const;

顺便说一句,还有许多其他的事情是行不通的,但是我让你去发现…

你必须用“
const
”限定符声明方法:

const char* get_name() const;

顺便说一句,还有很多其他的东西不起作用,但我让你去发现…

非常感谢,这解决了问题。但是我的方法不是修改顶点,我是否需要将其指定为常量?@nikhil:是的,因为在最一般的情况下,编译器不一定知道成员函数不会dify任何东西。此外,
const
作为良好的文档,还可以防止您无意中修改成员函数中的某些内容。嘿,非常感谢,这解决了问题。但是我的方法不是修改顶点。是否有必要将其指定为const?@nikhil:是的,因为在最一般的情况下在这种情况下,编译器不一定知道成员函数不会修改任何内容。此外,
const
作为良好的文档,还可以防止您无意中修改成员函数中的某些内容。如果我这样做,那么我将不得不更改我的大量代码,这是关于语法就是为什么要写两次常量,一次在开头,一次在结尾??第一个常量是函数返回类型声明的一部分,表示函数get_name()返回指向常量字符的指针。第二个常量表示函数get_name()是一个常量函数,即它不应该修改它所属对象中的任何数据