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

标签 c++ c linux

我的库(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 values\n");
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 类型是 opaque handle

所以,看来您的帖子并不完全真实,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 库,这样做会出现兼容性问题运行时比创建头文件的目的。

关于c++ - 为什么我在打印这个结构指针时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564521/

相关文章:

C++ ofstream输出到图像文件在Windows上写入不同的数据

c++ - 什么时候分配和初始化变量?

c++ - 这个 "print a heart"程序在 C 语言中是如何工作的?

c - 插入时对链表进行排序

linux - 从 headless 程序获取蓝牙事件

linux - 从 stdin 解释带有转义字符的字符串

c++ - 结合 std::vector Default 和 Fill 构造函数

c++ - 为什么我的计时器停止计时?

linux - Bash 转换多个子目录中的 PDF 文件

C - 使用 FFTW、fftw_plan_dft_1d 进行卷积