キューを用いた幅優先探索、構造体のソートの勉強をしています。起点ノードから他のノードへの距離を計算するために、 calc_distance関数の引数やキューの要素をNode構造体へのポインタとして、Nodeのメンバ変数degree, adj_nodes, nameを用いたプログラムを作りたいのですが、エラーが出てしまいコンパイルが出来ません。どこが間違えているか教えていただきたいです。
C
1#include <stdio.h> 2#include <stdlib.h> 3#include "graph_struct.h" 4#include "queue.h" 5 6Node** nodes_ptr; 7 8int *disV; //距離ベクトル 9Queue queue; //幅優先探索用のキュー 10int adj_nodes; 11Node degree; 12Node name; 13 14int init_value(){ 15 init_queue(&queue, N); 16 disV = (int *) malloc(sizeof(int)*N); 17} 18void calc_distance(nodes_ptr origin){ 19 queue.first = queue.last = 0; 20 enq(&queue, origin); 21 22 for(int i = 0; i < N; i++) disV[i] = id-1; 23 disV[id-1] = 0; 24 25 int maxD = 0; //最大距離 26 for(int d = 0; ; d++){ 27 int h1 = queue.first, h2 = queue.last; 28 for(int h = h1; h < h2; h++){ 29 30 nodes_ptr node = deq(&queue); 31 for(int k = 0; k < degree[node]; k++){ 32 nodes_ptr adj_node = adj_nodes[node][k]; 33 if(degree[adj_node] == -1){ 34 disV[adj_node] = d+1; 35 enq(&queue, adj_node); 36 } 37 } 38 } 39 if(queue.first == N || queue.first == queue.last){ maxD = d; break; } 40 } 41 return maxD; 42} 43 44 45void print_distance(char *fn, int D){ 46 FILE *fp = fopen(fn, "w"); 47 int *hashV = (int *) calloc(D+1, sizeof(int)); 48 for(int i = 0; i < N; i++) hashV[disV[i]] += 1; 49 int **hashM = (int **) malloc(sizeof(int *)*(D+1)); 50 for(int d = 0; d <= D; d++) 51 hashM[d] = (int *) malloc(sizeof(int)*hashV[d]); 52 for(int d = 0; d <= D; d++) hashV[d] = 0; 53 for(int i = 0; i < N; i++){ 54 int d = disV[i]; hashM[d][hashV[d]++] = i; 55} 56 57 58 59 60 for(int d = 0; d <= D; d++){ 61 fprintf(fp, "d=%d (%d駅)\n", d, hashV[d]); 62 for(int j = 0; j < hashV[d]; j++) 63 fprintf(fp, "%s ", name[hashM[d][j]]); 64 fprintf(fp, "\n-----------------------\n"); 65 } 66 fclose(fp); 67}
queue.hの中身
C
1typedef struct{ 2 Node** nodes_ptr; 3 int first; 4 int last; 5} Queue; 6 7void init_queue(Queue *queue, Node n){ 8 queue->nodes_ptr = (Node **) calloc(n, sizeof(Node)); 9 queue->first = queue->last = 0; 10 11} 12 13void enq(Queue queue, int nodes_ptr){ 14 queue->arr[queue->last++] = input; 15} 16 17int deq(Queue *queue){ 18 nodes_ptr output = queue->arr[queue->first++]; 19 return output; 20 21}
エラーメッセージ In file included from cpgm/distance.c:4:0: cpgm/queue.h: In function ‘init_queue’: cpgm/queue.h:8:39: error: incompatible type for argument 1 of ‘calloc’ queue->nodes_ptr = (Node **) calloc(n, sizeof(N ^ In file included from cpgm/distance.c:2:0: /usr/include/stdlib.h:468:14: note: expected ‘size_t {aka long unsigned int}’ but argument is of type ‘Node {aka struct <anonymous>}’ extern void *calloc (size_t __nmemb, size_t __s ^ In file included from cpgm/distance.c:4:0: cpgm/queue.h: In function ‘enq’: cpgm/queue.h:14:8: error: invalid type argument of ‘->’ (have ‘Queue {aka struct <anonymous>}’) queue->arr[queue->last++] = input; ^ cpgm/queue.h:14:19: error: invalid type argument of ‘->’ (have ‘Queue {aka struct <anonymous>}’) queue->arr[queue->last++] = input; ^ cpgm/queue.h:14:31: error: ‘input’ undeclared (first use in this function) queue->arr[queue->last++] = input; ^ cpgm/queue.h:14:31: note: each undeclared identifier is reported only once for each function it appears in cpgm/queue.h: In function ‘deq’: cpgm/queue.h:18:3: error: unknown type name ‘nodes_ptr’ nodes_ptr output = queue->arr[queue->first++] ^ cpgm/queue.h:18:27: error: ‘Queue {aka struct <anonymous>}’ has no member named ‘arr’ nodes_ptr output = queue->arr[queue->first++] ^ cpgm/distance.c: In function ‘init_value’: cpgm/distance.c:15:22: error: incompatible type for argument 2 of ‘init_queue’ init_queue(&queue, N); ^ In file included from cpgm/distance.c:4:0: cpgm/queue.h:7:6: note: expected ‘Node {aka struct <anonymous>}’ but argument is of type ‘int’ void init_queue(Queue *queue, Node n){ ^ cpgm/distance.c: At top level: cpgm/distance.c:18:20: error: expected declaration specifiers or ‘...’ before ‘nodes_ptr’ void calc_distance(nodes_ptr origin){ ^ cpgm/distance.c: In function ‘print_distance’: cpgm/distance.c:63:30: error: subscripted value is neither array nor pointer nor vector fprintf(fp, "%s ", name[hashM[d][j]]); ^ cpgm/distance.c: In function ‘main’: cpgm/distance.c:74:14: warning: implicit declaration of function ‘calc_distance’ [-Wimplicit-function-declaration] int maxD = calc_distance(origin);
エラーメッセージを提示しよう