2016-03-29 7 views
-1

構造体をtypedefしようとしているときに、次のエラーが表示されます。私は前にこれをやったし、前と同じフォーマットをしているが、何かが動かず、完全に困惑している。Typedef C Struct:不完全なtypedefの無効な使用

Shm_channel.h:

​​

Shm_channel.c:

// Struct that contains all the message queue information 
struct _msgQ_info { 
    mqd_t descriptor; 
    mode_t mode; 
    char *name; 
}; 

Other_file.c:

#include <errno.h> 
#include <getopt.h> 
#include <signal.h> 
#include <strings.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <signal.h> 
#include <mqueue.h> 

#include "shm_channel.h" 

//... Inside of Main() 
    msgQ_info msgQinfo; 
     msgQinfo = init_message_queue(); 
     if(0 > open_message_queue(&msgQinfo)){ 
     fprintf(stderr, "message queue descriptor failed to be initialized in webproxy.c\n"); 
     return 0; 
     }else{ 
     fprintf(stderr, "Message queue descriptor successfully created with value : %d\n", msgQinfo.descriptor); 
     } 

エラー:

enter image description here Other_file.cで

+1

テキストの画像を投稿しないでください! – Olaf

+0

構造体の大きさを知るための 'Other_file.c'のコンパイラはどのようになっていますか?それはどのレイアウトですか? – Olaf

+0

完全な構造体定義を最初に指定することなく、構造体型の変数を宣言することはできません。それ以外の場合、コンパイラはどのくらいの量のメモリを割り当てるのか分かりません。したがって、構造体定義を '.c'ファイルから' .h'ファイルに移動します。 –

答えて

-1

がOther_file.c

#include "shm_channel.h" 

#include "shm_channel.c" 

を追加するには、コンパイラは、 "不完全な型定義" としてmsgQ_info考えました。 struct _msgQ_infoの宣言はすでにshm_channel.cに存在していたので、#includeにする必要があります。


また、shm_channel.hに宣言を追加します。

// Struct that contains all the message queue information 
struct _msgQ_info { 
    mqd_t descriptor; 
    mode_t mode; 
    char *name; 
}; 

typedef struct _msgQ_info msgQ_info; 
/* 
* This function initializes and returns a mesQ_info struct for 
* the user 
*/ 
msgQ_info init_message_queue(); 

それはあなたのプロジェクトは、より明確にするために、私は個人的には、第二のアプローチを好みます。

-1

"shm_channel.c" と

を "shm_channel.h" を交換し、shm_channel.cの先頭に持っている:

#include "shm_channel.h" 

どれ.hファイルが中に含まれるべき同じ(修飾されていない)名前の.cファイル。あなたは何struct _msgQ_infoであることを教えていなかったので

2

msgQ_infoは、不透明タイプを意図していますか?もしそうなら、あなたはShm_channel.cの外側からそれを改ざんすべきではありません。

このような設計の理由を考えてみましょう...作成者が非ポータブルな内部構造が抽象概念を越えて漏れて、移植可能なコードであると思われる可能性があると思いますか?

あなたはそれを改ざんすることにしますか?Shm_channel.cの枠内で、構造体(ポータブルではない?)の内部が隔離されているはずです。

関連する問題