为什么我在打印这个结构指针时出错

Why do I get error in printing this structure pointer?

本文关键字:结构 指针 出错 打印 为什么      更新时间:2023-10-16

我的库(amqp C库)有一个名为amqp.h的.h文件,它包含以下内容:

typedef struct amqp_connection_state_t_ *amqp_connection_state_t;
struct amqp_connection_state_t_ {
  amqp_pool_t frame_pool;
  amqp_pool_t decoding_pool;
  amqp_connection_state_enum state;
  int channel_max;
  int frame_max;
  int heartbeat;
  amqp_bytes_t inbound_buffer;
  size_t inbound_offset;
  size_t target_size;
  amqp_bytes_t outbound_buffer;
  int sockfd;
  amqp_bytes_t sock_inbound_buffer;
  size_t sock_inbound_offset;
  size_t sock_inbound_limit;
  amqp_link_t *first_queued_frame;
  amqp_link_t *last_queued_frame;
  amqp_rpc_reply_t most_recent_api_result;
};

我正试图在我的本地测试程序中打印结构的上述值:

amqp_connection_state_t state;
state = conn->getConnectionState( );
printf("Connection state valuesn");
printf("Channel max: %d", state->channel_max);
printf("frame max: %d", state->frame_max);
printf("sockfd: %d", state->sockfd);

反过来,我得到了以下编译错误:

amqpoc.cpp: In function âvoid* con(void*)â:
amqpoc.cpp:85: error: invalid use of incomplete type âstruct amqp_connection_state_t_â
../common/amqp.h:294: error: forward declaration of âstruct amqp_connection_state_t_â
amqpoc.cpp:86: error: invalid use of incomplete type âstruct amqp_connection_state_t_â
../common/amqp.h:294: error: forward declaration of âstruct amqp_connection_state_t_â
amqpoc.cpp:87: error: invalid use of incomplete type âstruct amqp_connection_state_t_â
../common/amqp.h:294: error: forward declaration of âstruct amqp_connection_state_t_â
amqpoc.cpp:88: error: invalid use of incomplete type âstruct amqp_connection_state_t_â
../common/amqp.h:294: error: forward declaration of âstruct amqp_connection_state_t_â

问题出在哪里?

struct amqp_connection_state_t_供内部使用。你不应该直接访问它。代码处理的amqp_connection_state_t类型是一个不透明的句柄

所以,你的帖子似乎并不完全真实,struct amqp_connection_state_t_声明不在你包含的头文件中,它在amqp_private.h文件中,但你包含了amqp.h

如果你想获得channel_max,有一个访问功能:

  printf("Channel max: %d", amqp_get_channel_max(state));

->sockfd成员被暴露为具有amqp_get_sockfd功能。但是->frame_max似乎没有暴露,所以您无法获取它。

如果您还包括amqp_private.h,您可能可以直接访问这些成员,请注意,如果您在运行时使用与创建头文件不同的amqp库版本,则在这样做时会出现兼容性问题。

我认为问题出在下面的命令上:

state = conn->getConnectionState( );

您确定getConnectionState()函数会返回amqp_connection_state_t类型吗。当然,您应该使用state = (amqp_connection_state_t)conn->getConnectionState( );