【问题标题】:Dijkstra Program迪杰斯特拉计划
【发布时间】:2019-11-13 22:02:54
【问题描述】:

我在使用之前在不同程序中使用过的函数时遇到了问题,并且在将它们转换为在这个新程序中使用时遇到了困难。 Dijkstra.cpp 从标准输入中读取有关加权图的信息 并打印输入图的描述,即从 开始顶点和结束顶点,以及该路径到 标准输出。

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

#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);
    }
}

【问题讨论】:

  • Edge newEdge; 调用默认构造函数。您创建了一个自定义构造函数,因此编译器不会创建默认构造函数。创建您自己的默认构造函数或使用Edge newEdge(u, v, w, nullptr); 调用您的自定义构造函数。错误消息非常清楚。阅读它们并按照编译器的建议进行操作。
  • 错误的数量远远超出了问一个问题的范围。关注第一个(如果它们看起来相关,也可以在第一个之后发布一两个)。将代码修剪为minimal reproducible example。此外,将错误消息复制为文本而不是图像会(对您来说更容易,并且会)让更广泛的受众了解您的问题。

标签: c++ dijkstra


【解决方案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。查看触发错误的行。

有时在那条线(或者它上面的线)会有一些明显的东西。缺少分号、被遗忘的论点、错字等。

您的编译器已告诉您在哪里查找。错误位于行号152dijkstra.cpp 中。它还为我们提供了它所在函数的签名:void insertEdge(int, int, int, Graph*)。事实上,它甚至可以确定它意识到哪里出了问题:当你的程序试图创建一个名为 newEdgeEdge 时。

2。解读错误信息。

对于不明显的错误,您可能需要深入了解错误消息的含义。在这种情况下,列出的错误是:no matching function for call to 'Edge::Edge()'

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

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

3。修复错误

对于此特定错误,您可以更改引用的行以使用参数创建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
};

struct Edge 
{
    ...
    Edge(int u = 0, int v = 0, double w = 0.0, Edge* nextEdge = nullptr) // Defines default values so that constructor can be called with 0-4 arguments
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
};

4。重复

现在你重新编译你的程序。希望错误消息的数量有所减少。不管怎样,你查看列表中的第一个错误,然后重复这个过程,直到你的程序编译没有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    相关资源
    最近更新 更多