2017-08-28 14 views
1

次のコードを実行しようとすると、逆参照ポインタが不完全型エラーになります。私はこのエラーに関するいくつかの他の質問をチェックしました。それは、構造体キーワードがないか、余分なものが原因ではないことがわかります。ポインタの型は正しいと思いますが間違います。関数ポインタを提供するときに不完全型へのポインタを参照解除する

私はC言語を学んでいるので、コードに他の問題があるかもしれません。私は自分自身のためにそれらを試してみてうれしく、不完全なタイプのエラーで問題を追跡できないようです。

Development/C/AI/test/src/test.c: In function ‘compare’: 
Development/C/AI/test/src/test.c:10:19: error: dereferencing pointer to incomplete type ‘lrgraph_node {aka struct lrgraph_node}’ 
    if (strcmp(other->dataType, current->dataType == 0)) { 

test.cの

#include "lrGraph.h" 
#include <string.h> 

int data = 1; 
char *dataType = "int"; 
lrgraph_edge *connected[] = {}; 
unsigned numEdges = 0; 

int compare(lrgraph_node *other, lrgraph_node *current) { 
    if (strcmp(other->dataType, current->dataType == 0)) { 
     return (int)other->data - (int)current->data; 
    } 
    return -1; 
} 

int main() { 
    lrgraph_node *nodeA = lrgraph_createNode((void*)&data, dataType, &compare, connected, numEdges); 
    lrgraph_printVersion(); 
} 

lrGraph.c

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "lrGraph.h" 

struct lrgraph_edge { 
    float weight; 
    lrgraph_node *nodeA; 
    lrgraph_node *nodeB; 
}; 

struct lrgraph_node { 
    //data can be of any type 
    void *data; 
    //string to see if this node can be compared to another node based on data type 
    char *dataType; 
    int numEdges; 
    //comparator function which compares another node to this node 
    int (*compare)(lrgraph_node *other, lrgraph_node *current); 
    //array of connected edges 
    lrgraph_edge *connected[]; 
}; 

void lrgraph_printVersion() { 
    fprintf(stdout, "\nlrgraph version 0.01b\n"); 
} 


lrgraph_node* lrgraph_createNode(void *data, char *dataType, int (*compare)(lrgraph_node* other, lrgraph_node* current), lrgraph_edge *connected[], unsigned numEdges) { 
    //allocate enough memory for the struct plus each pointer in the array of edges - https://stackoverflow.com/questions/32311269/can-we-have-a-struct-element-of-type-variable-length-array 
    lrgraph_node *node = malloc(sizeof(lrgraph_node) + numEdges * sizeof(lrgraph_edge)); 
    if (NULL != node) { 
     node->data = data; 
     node->dataType = strdup(dataType); 
     node->compare = compare; 
     node->numEdges = numEdges; 
     //initialize each edge in the array 
     for(unsigned i=0; i < numEdges; i++) { 
      node->connected[i] = connected[i]; 
     } 
    } 
    return node; 
} 

lrGraph.h

#ifndef LRGRAPH_H 
#define LRGRAPH_H 

typedef struct lrgraph_node lrgraph_node; 

typedef struct lrgraph_edge lrgraph_edge; 

lrgraph_node* lrgraph_createNode(void *data, char *dataType, int (*compare)(lrgraph_node *other, lrgraph_node *current), lrgraph_edge *connected[], unsigned numEdges); 

void lrgraph_printVersion(); 

#endif /*LRGRAPH_H*/ 
+1

'lgraph_node'の定義はありません。それはどこに定義されていますか? –

+0

lrGraph.hとlrGraph.c(私が間違って定義した場合を除き)問題は – SMC

+2

'lrGraph.c'に型定義を置きました。型は 'lrGraph.c'でのみ完成します。他のすべての '.c'ファイルでは、それらは不完全です。なぜ型定義を 'lrGraph.c'に置いたのですか?あなたの型を複数の '.c'ファイルで完全にするには、定義はこれらの' .c'ファイルすべてにすぐになければなりません。つまり、ヘッダファイルに存在するはずです。 – AnT

答えて

4

"不完全なタイプは、" コンパイラはあなたがしようとしている見ている意味しますstruct型を使用しますが、その構造体の定義はありませんct。

これは、構造体へのポインタのみを使用している場合(実際にCで抽象データ型が実装されている場合)は問題ありませんが、そのようなポインタを逆参照したい場合は構造体定義を表示する必要があります。 test.c

のみlrGraph.hの内容(すなわちtypedef struct lrgraph_node lrgraph_node;)表示され、実際struct lrgraph_node { ... };定義のみlrGraph.cに存在します。

考えられる解決策:struct lrgraph_node { ... }定義をヘッダーに移動します。 (またはcompareの定義をlrGraph.cに入力します)。

+0

これはなぜ問題が存在するのですが、Cで構造体の実装を隠す方法があります。その構造体を使用する?私はここでqsortのようなことをやろうとしていましたが、ユーザがノードを作成するときにコンパレータを提供します。 – SMC

+1

@SMC 'lrGraph.c'にノードポインタの' data'と 'dataType'を返す関数を作成して'lrGraph.h'の中で)これらの関数を' test.c'で使うことができます。また、他の人ができるようにするために、グラフのトラバーサル関数や関数が必要な場合もあります。 –

関連する問題