gpt4 book ai didi

c - 错误: dereferencing pointer to incomplete type when compil C program

转载 作者:行者123 更新时间:2023-11-30 20:25:47 24 4
gpt4 key购买 nike

我在这里放了一个简短的源代码,因为源代码太长了。你可以在这个 git 存储库中找到完整的源代码:https://github.com/strophe/libstrophe

事实上,我在我的主 C 程序中使用 strope 作为库,它是一个 openWrt 包。

common.h(libstrope)的源代码

/** run-time context **/

typedef enum {
XMPP_LOOP_NOTSTARTED,
XMPP_LOOP_RUNNING,
XMPP_LOOP_QUIT
} xmpp_loop_status_t;

typedef struct _xmpp_connlist_t {
xmpp_conn_t *conn;
struct _xmpp_connlist_t *next;
} xmpp_connlist_t;

struct _xmpp_ctx_t {
const xmpp_mem_t *mem;
const xmpp_log_t *log;

xmpp_loop_status_t loop_status;
xmpp_connlist_t *connlist;
};

这是头文件 strope.h 源代码(libstropic):

/
* user-replaceable memory allocator */
typedef struct _xmpp_mem_t xmpp_mem_t;

/* user-replaceable log object */
typedef struct _xmpp_log_t xmpp_log_t;

/* opaque run time context containing the above hooks */
typedef struct _xmpp_ctx_t xmpp_ctx_t;

xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem,
const xmpp_log_t * const log);
void xmpp_ctx_free(xmpp_ctx_t * const ctx);
struct _xmpp_log_t {
xmpp_log_handler handler;
void *userdata;
/* mutex_t lock; */
};

ctx.c简要源代码(libstrope):

xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem, 
const xmpp_log_t * const log)
{
xmpp_ctx_t *ctx = NULL;

if (mem == NULL)
ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t), NULL);
else
ctx = mem->alloc(sizeof(xmpp_ctx_t), mem->userdata);

if (ctx != NULL) {
if (mem != NULL)
ctx->mem = mem;
else
ctx->mem = &xmpp_default_mem;

if (log == NULL)
ctx->log = &xmpp_default_log;
else
ctx->log = log;

ctx->connlist = NULL;
ctx->loop_status = 0;//XMPP_LOOP_NOTSTARTED;
}

return ctx;
}

主C程序

#include <strophe.h>

void main()
{
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
char *jid, *pass;


create a context */
log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
ctx = xmpp_ctx_new(NULL, log);
/* create a connection */
conn = xmpp_conn_new(ctx);
/* setup authentication information */
xmpp_conn_set_jid(conn, "jid");
xmpp_conn_set_pass(conn, "pass");
/* initiate connection */
xmpp_connect_client(conn, "alt3.xmpp.l.google.com", 5222, conn_handler, ctx);
ctx->loop_status = 1; // error error: dereferencing pointer to incomplete type
/* enter the event loop -
our connect handler will trigger an exit */
xmpp_run(ctx);
/* release our connection and context */
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
/* final shutdown of the library */
xmpp_shutdown();
}

使用此源代码编译时出现此错误:

error: dereferencing pointer to incomplete type

最佳答案

是循环依赖头问题。

文件 strope.h 必须包含在 common.h 之前

由于 stropice.h 中的类型必须被 common.h 所识别

#include "strophe.h"
#include "common.h"

void main()
{
xmpp_ctx_t *ctx;

xmpp_initialize();

ctx = xmpp_ctx_new(NULL, log);
/* create a connection */
conn = xmpp_conn_new(ctx);
xmpp_connect_client(conn, "alt3.xmpp.l.google.com", 5222, conn_handler,
ctx);
ctx->loop_status = 1; // error error: dereferencing pointer to incomplete type
xmpp_run(ctx);
}

关于c - 错误: dereferencing pointer to incomplete type when compil C program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27568645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com