【发布时间】: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。此外,将错误消息复制为文本而不是图像会(对您来说更容易,并且会)让更广泛的受众了解您的问题。