写邻接列表图时出现未知错误

Unknown Error While Writing An Adjacent List Graph

本文关键字:未知 错误 列表      更新时间:2023-10-16

我正在编写一个带加权边的相邻列表图。我的目标是实现一个图来测试Djikstra的最短路径算法。当我实现removeEdge函数时,我感到很失望。我查看了构建消息,但我不知道下面的错误是关于什么的。

在下面这个之前有一些警告,但它们是次要的,因为它编译和运行正常。

c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1includec++bitslist。tcc||在实例化'void std::list<_Tp, _Alloc>::remove(const value_type&) [with _Tp = Edge;_Alloc = std::allocator;std:: list<_Tp _Alloc>:: value_type =]"边缘:|

这是生成的错误。

c:program files (x86)codeblocksmingwbin..libgccmingw324.7.1includec++bitslist。太极拳| 249 |错误:无法与"运算符= ="__first.std: _List_iterator<_Tp>::操作符* ()= = __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

请注意这张图是不完整的。许多功能仍然需要在那里实现。有人知道这个数据结构代码有什么问题吗?

您还没有为Edge提供operator==,我不知道您究竟想要如何指定哪些Edge s是相等的,但是在使用remove

之前,您需要以下定义的东西
bool operator==(Edge const& lhs, Edge const& rhs)
{
  return
  lhs.getWeight()  == rhs.getWeight() &&
  lhs.getSrcNum()  == rhs.getSrcNum() &&
  lhs.getDestNum() == rhs.getDestNum();
}