我正在写一个带有加权边的相邻的基于列表的图。我的目标是实现一个图形来测试Djikstra的最短路径算法。当我实现removeEdge函数时,我感到非常震惊。我查看了构建消息,但没有以下错误的线索。
在此之前,有一些警告,但由于它经过编译和运行还不错。
c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\list.tcc||In instantiation of 'void std::list<_Tp, _Alloc>::remove(const value_type&) [with _Tp = Edge; _Alloc = std::allocator; std::list<_Tp, _Alloc>::value_type = Edge]':|
这是生成的错误。
c:\program files (x86)\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\list.tcc|249|error: no match for 'operator==' in '__first.std::_List_iterator<_Tp>::operator*() == __value'|
现在,代码:
#ifndef WEIGHTED_ADJACENT_LIST_GRAPH_H
#define WEIGHTED_ADJACENT_LIST_GRAPH_H
#include <list>
#include <forward_list>
#include <stack>
#include <string>
using namespace std;
typedef int Weight;
class Edge;
class Vertex {
friend class Edge;
int num;
string name;
public:
Vertex();
Vertex(int n, string v_name){
num = n;
name = v_name;
}
int getNum() const{
return num;
}
string getName() const{
return name;
}
void setNum(int new_num){
num = new_num;
}
void setName(string new_name){
name = new_name;
}
};
class Edge {
Weight weight;
Vertex src;
Vertex dest;
public:
Edge();
Edge(Vertex s, Vertex d, Weight w):src(s), dest(d),weight(w){}
/*
Edge(Vertex s, Vertex d, Weight w){
src = s;
dest = d;
weight = w;
}
*/
Weight getWeight() const{
return weight;
}
int getSrcNum() const{
return src.num;
}
int getDestNum() const{
return dest.num;
}
};
class AdjacentList{
int num_Vertices;
list<Edge> *adj;
public:
AdjacentList();
AdjacentList(int n){
num_Vertices = n;
}
void addEdge(Vertex &i, Edge &j){
adj[i.getNum()].push_back(j);
}
void removeEdge(Vertex &i, Edge j){
if(!adj[i.getNum()].empty())
{
adj[i.getNum()].remove(j);
}
else{
cerr<<"Adjacent list underflow in removeEdge function"<<endl;
}
}
};
#endif
请注意,此图不完整。那里仍然需要实现很多功能。有谁知道这个数据结构代码怎么了?
最佳答案
您尚未提供operator==
的Edge
,我不知道如何确切地指定哪些Edge
相等,但是在使用remove
之前,您需要定义如下内容
bool operator==(Edge const& lhs, Edge const& rhs)
{
return
lhs.getWeight() == rhs.getWeight() &&
lhs.getSrcNum() == rhs.getSrcNum() &&
lhs.getDestNum() == rhs.getDestNum();
}
关于c++ - 编写相邻列表图形时发生未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163029/