我正在嘗試printPath在另一個函式 ( ) 中撰寫遞回函式 ( dijkstraSSSP),但它給了我一個編譯器錯誤。
當我運行時gcc dijkstra.c WGraph.c PQueue.c,它編譯得很好。但是,當我運行dcc -Wall -Werror -std=c11 -o dijkstra dijkstra.c WGraph.c PQueue.c. 我收到了這個錯誤:
dijkstra.c:48:38: error: function definition is not allowed here
void printPath (int currentNode) {
我認為這是因為我在另一個函式中定義了一個函式。如果是這種情況,我應該如何修改代碼?非常感謝!
void dijkstraSSSP(Graph g, Vertex source) {
int dist[MAX_NODES];
int pred[MAX_NODES];
bool vSet[MAX_NODES]; // vSet[v] = true <=> v has not been processed
int s, t;
PQueueInit();
int nV = numOfVertices(g);
for (s = 0; s < nV; s ) {
joinPQueue(s);
dist[s] = VERY_HIGH_VALUE;
pred[s] = -1;
vSet[s] = true;
}
dist[source] = 0;
int relaxWeight;
for (s = 0; s < numOfVertices(g); s ) {
s = leavePQueue(dist);
//printf("iterating with s = %d\n", s);
//printf("updating vSet[%d] = false\n", s);
vSet[s] = false;
for (t = 0; t < numOfVertices(g); t ) {
//printf("iterating with t = %d\n", t);
//printf("checking if s = %d is adj to t = %d and vSet[%d] = true\n", s, t, t);
if (adjacent(g, s, t) != 0 && vSet[t] == true) {
//printf("YES\n");
relaxWeight = adjacent(g, s, t);
if (dist[t] > dist[s] relaxWeight) {
//printf("updating dist[%d] = dist[%d] adjWeight[%d]\n", t, s, relaxWeight);
dist[t] = dist[s] adjacent(g, s, t);
//printf("updating pred[%d] = %d\n", t, s);
pred[t] = s;
}
}
}
}
void printPath (int currentNode) {
if (pred[currentNode] == -1) {
printf("%d", currentNode);
return;
} else {
printPath (pred[currentNode]);
printf("-%d", currentNode);
}
}
for (int i = 0; i < numOfVertices(g); i ) {
if (dist[i] == VERY_HIGH_VALUE) {
printf("%d: no path\n", i);
} else{
printf("%d: distance = %d, shortest path: ", i, dist[i]);
printPath(i);
printf("\n");
}
}
}
uj5u.com熱心網友回復:
在 C 標準中沒有定義在另一個函式中定義一個函式。gcc允許它作為擴展,但大多數編譯器不允許。
您應該將定義移到函式printPath體之外dijkstraSSSP,在源代碼中它之前,并pred作為額外引數傳遞:
void printPath(const int *pred, int currentNode) {
if (pred[currentNode] == -1) {
printf("%d", currentNode);
} else {
printPath(pred, pred[currentNode]);
printf("-%d", currentNode);
}
}
void dijkstraSSSP(Graph g, Vertex source) {
int dist[MAX_NODES];
int pred[MAX_NODES];
bool vSet[MAX_NODES]; // vSet[v] = true <=> v has not been processed
int s, t;
PQueueInit();
int nV = numOfVertices(g);
for (s = 0; s < nV; s ) {
joinPQueue(s);
dist[s] = VERY_HIGH_VALUE;
pred[s] = -1;
vSet[s] = true;
}
dist[source] = 0;
int relaxWeight;
for (s = 0; s < numOfVertices(g); s ) {
s = leavePQueue(dist);
//printf("iterating with s = %d\n", s);
//printf("updating vSet[%d] = false\n", s);
vSet[s] = false;
for (t = 0; t < numOfVertices(g); t ) {
//printf("iterating with t = %d\n", t);
//printf("checking if s = %d is adj to t = %d and vSet[%d] = true\n", s, t, t);
if (adjacent(g, s, t) != 0 && vSet[t] == true) {
//printf("YES\n");
relaxWeight = adjacent(g, s, t);
if (dist[t] > dist[s] relaxWeight) {
//printf("updating dist[%d] = dist[%d] adjWeight[%d]\n", t, s, relaxWeight);
dist[t] = dist[s] adjacent(g, s, t);
//printf("updating pred[%d] = %d\n", t, s);
pred[t] = s;
}
}
}
}
for (int i = 0; i < numOfVertices(g); i ) {
if (dist[i] == VERY_HIGH_VALUE) {
printf("%d: no path\n", i);
} else{
printf("%d: distance = %d, shortest path: ", i, dist[i]);
printPath(pred, i);
printf("\n");
}
}
}
uj5u.com熱心網友回復:
將定義移到 andprintPath的主體之外dijkstraSSSP,使其能夠訪問pred,將其更改為接受兩個引數而不是一個。附加引數應該有型別int *并且應該指向 的第一個元素pred。
在呼叫中,傳遞pred該新引數。該陣列pred將自動轉換為指向其第一個元素的指標。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448945.html
上一篇:為什么我的遞回搜索功能不起作用?
