繁体   English   中英

Typedef C 结构:无效使用不完整的 typedef

[英]Typedef C Struct: invalid use of incomplete typedef

尝试对结构进行 typedef 时出现以下错误。 我以前做过这件事,并且遵循与以前完全相同的格式,但有些东西不起作用,我完全被难住了。

shm_channel.h:

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

shm_channel.c:

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

其他_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);
      }

错误:

在此处输入图片说明

msgQ_info是否旨在成为不透明类型? 如果是这样,您不应该从 Shm_channel.c 外部篡改它。

考虑一下这种设计的原因……你认为作者是否可能试图防止不可移植的内部结构泄漏到抽象之外并进入应该是可移植的代码?

如果您决定篡改它,您可能应该在 Shm_channel.c 的范围内这样做,其中结构(不可移植?)内部是隔离的。

在 Other_file.c 中将“shm_channel.h”替换为“shm_channel.c”

并且在shm_channel.c的开头有:

#include "shm_channel.h"

任何 .h 文件都应包含在具有相同(非限定)名称的 .c 文件中。

添加

#include "shm_channel.c"

#include "shm_channel.h"

在 Other_file.c 中

您的编译器将msgQ_info视为“不完整的 typedef”,因为您没有告诉它struct _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();

我个人更喜欢第二种方法,因为它使您的项目更加清晰。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM