c++ - 编写相邻列表图形时发生未知错误

标签 c++ compiler-errors graph-algorithm adjacency-list

我正在写一个带有加权边的相邻的基于列表的图。我的目标是实现一个图形来测试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/

相关文章:

algorithm - 最大权重欧氏生成树

MST 的 Kruskal 算法的 C 实现

c++ - 为什么我不能执行 std::map.begin() + 1?

c++ - 在构建我的 C++ 可执行文件 (gcc) 时,我可以获得所有链接库的报告吗? (包括静态链接)

c++ - 错误 : cannot convert Elem to Elem* for argument 1 to void addHead(Elem*, 元素 *)

c# - C#错误CS1513和CS1023

c# - 边缘成本不对称时的最近邻居,一些疑问

c++ - std::vector::reserve 是否保证在这种情况下实现不会使迭代器无效?

c++ - 如何将可变参数转换为容器类?

visual-c++ - C++语法错误: missing ';' before 'constant'