迪克斯特拉计划

时间:2019-11-13 22:02:54

标签: c++ dijkstra

我在以前在不同程序中使用过的功能遇到了麻烦,并且遇到了将它们转换为在新程序中使用的障碍。 Dijkstra.cpp从标准输入中读取有关加权图的信息 并打印输入图的描述,即距图的最短路径 起始顶点和终止顶点,以及该路径到 标准输出。

这是我收到的 ALL 错误(因为它是一个不完整的程序):

attach1 attach2

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int trace = 0;

//An object of type Edge is a cell in an adjacency list and represents an edge
//of the graph.

struct Edge 
{
    int from;           //The 'from' direction of the edge.
    int to;         //The 'to' direction of the edge.
    double weight;      //The weight of the edge from u to v.
    Edge* next;     //A pointer to the next edge in the linked list.

    Edge(int u, int v, double w, Edge* nextEdge)
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
};


//An object of type Vertex represents information about one vertex in a graph.

struct Vertex       
{
    Edge* edgeList;     //A pointer to a linked list of all edges 
                    //from v to another vertex.

    double time;        //A real number representing the shortest
                    //distance from the start vertex.

    int sender;     //Used with the above real number, it begins 
                //finding the shortest path starting from v to
                //the sender. 

    Vertex()
    {
        edgeList = NULL;
        time = -1;
        sender = -1;
    }
};


//An object of type graph represents a weighted graph.

struct Graph
{
    int numVertices;    //The number of verticies in the graph.
    int numEdges;       //The number of edges in the graph.

    Vertex* vertices;   //An array for verticies utilizing a Vertex
                        //structure that gives information about a
                        //specific vertex.

    Graph(int nV)
    {
        numVertices = nV;
        vertices = new Vertex[nV+1];
        numEdges = 0;
    }
};



//insertEdge inserts an edge into the graph g.

void insertEdge(int u, int v, int w, Graph* g)
{
    Edge newEdge;
    newEdge.from = u;
    newEdge.to = v;
    newEdge.weight = w;

    if(g->numVertices > g->numEdges)
    {   
        g->[g->numEdges] = newEdge;
        g->numEdges++;
    }
}

//insertOpposite inserts an edge going the opposite way
//as insertEdge does into graph g.

void insertOpposite(int u, int v, int w, Graph* g)
{
    Edge newEdge;
    newEdge.from = v;
    newEdge.to = u;
    newEdge.weight = w;

    if(g->numVertices > g->numEdges)
    {       
        g->vertices[g->numEdges] = newEdge;
        g->numEdges++;
    }
}


//readGraph reads a graph and is also able to insert edges into a graph.

Graph* readGraph()
{
    int nV, u,v,w, edges = 0;

    scanf("%i", &nV);

    Graph* g = new Graph(nV);

    while(true)
    {
        scanf("%i", &u);
        if(u == 0)
        {
                break;
        }

        scanf("%i", &v);
        scanf("%i", &w);

        insertEdge(u, v, w, g);
        insertOpposite(u, v, w, g);

        edges++;
    }

    g->totalEdges = edges;  

    return g;
}


//writeGraph prints a formatted chart from graph g 
//that includes the amount of vertices and edges along with 
//the weight that corresponds to each edge.

void writeGraph(Graph* g)
{
    printf("\nThere are %i vertices and %i edges\n", g->numVertices,
        g->numEdges);

    printf("\n The edges are as follows.");

    for(int n = 0; n < g->totalEdges; n++)
    {
        printf("(%i,", g->vertices[n].vertex1);
        printf("%i)  ", g->vertices[n].vertex2);
        printf("weight %3i\n", g->vertices[n].weight);
    }
}

1 个答案:

答案 0 :(得分:1)

我不会详尽地介绍每个错误,但这是解决这些错误的通用策略。

当处理来自编译器的错误消息时,从第一个错误开始,对其进行处理,然后尝试再次编译,通常是一个好习惯。通常,一个问题会导致出现多个错误消息,而解决一个问题可以为其余问题提供线索。如果没有其他问题,那么一次处理一件事有助于使事情易于管理。

让我们看看第一个错误消息:

dijkstra.cpp: In function ‘void insertEdge(int, int, int, Graph*)
dijkstra.cpp:152:10: error: no matching function for call to ‘Edge::Edge()’
    Edge newEdge;
         ^

1。查看触发错误的行。

有时在该行(或它上方的行)会有明显的东西。缺少分号,被遗忘的论点,错字等。

您的编译器已告诉您在哪里查看。错误在dijkstra.cpp行号152中。它还为我们提供了位于void insertEdge(int, int, int, Graph*)中的函数的签名。实际上,它甚至可以精确地指出出现问题的地方:当您的程序尝试创建名为Edge的{​​{1}}时。

2。解释错误消息。

对于非显而易见的错误,您可能必须深入研究错误消息在说什么。在这种情况下,列出的错误是:newEdge

对我来说,这似乎很不言自明,但是当然,这取决于阅读邮件的人的经验水平。如果我不理解错误消息,请采用一种简单的策略:Google搜索。删除特定于您的程序的名称将很有用,因此在这种情况下,我可能会搜索no matching function for call to 'Edge::Edge()'。这样做为面临类似错误的程序员带来了很多结果。

在这种特定情况下,编译器说您正在尝试调用函数"error: no matching function for call to",但找不到与之匹配的函数。 Edge::Edge()将是您的Edge::Edge()结构的构造函数-但您仅定义了一个构造函数,并且需要多个参数,因此编译器不知道如何构造一个Edge任何参数。

3。解决错误

对于此特定错误,您可以更改引用的行以使用参数创建Edge。像这样:

Edge

但这可能不是您想要的。您更可能希望向Edge newEdge(0, 0, 0.0, nullptr) 结构中添加默认构造函数。

Edge

struct Edge 
{
    ...
    Edge(int u, int v, double w, Edge* nextEdge)
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
    Edge() = default; // Creates a default constructor; leaves values uninitialized
};

struct Edge 
{
    ...
    Edge(int u, int v, double w, Edge* nextEdge)
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
    Edge() : from(0), to(0), weight(0.0), next(nullptr) {} // Initializes members to a set of defaults
};

4。重复

现在,您重新编译程序。希望错误消息的数量已减少。无论如何,您都会查看列表中的第一个错误,然后重复此过程,直到程序编译没有错误为止。