C++ 为什么打印此结构指针时出错?

C++ 为什么打印此结构指针时出错?,c++,c,linux,C++,C,Linux,My library(amqp C library)有一个名为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; i

My library(amqp C library)有一个名为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_â

问题出在哪里?

我认为问题出在以下方面:

state = conn->getConnectionState( );
您确定
getConnectionState()
函数将返回amqp\u连接状态类型吗。
当然,您应该使用
state=(amqp\u connection\u state\t)conn->getConnectionState()

结构amqp\u连接\u状态\u t\u
供内部使用。您不应该直接访问它。代码处理的amqp\u连接\u状态\u类型是

因此,您的帖子似乎并不完全真实,
struct amqp\u connection\u state\u t\u声明不在您包含的头文件中,它在
amqp\u private.h
文件中,但您包含
amqp.h

如果您想获取
频道\u max
,有一个访问功能:

  printf("Channel max: %d", amqp_get_channel_max(state));
->sockfd
成员通过
amqp\u get\u sockfd
功能公开<代码>->frame_max
似乎没有公开,所以您无法获取它


如果你还包含了<>代码> AMQPU.Prime.H./C++ >,你可以直接访问这些成员,当你在运行时使用AMQP库的不同版本而不是创建头文件时,你会意识到会有兼容性问题。选择一个。我们使用的是RabByMQ C库,上面有一个C++包装器。给出错误的测试代码被编译成C++代码,你所共享的代码是85, 86, 87行和88行。这只是4个连续的数字让我觉得它是有错误的其他行。amqp.h中的第294行是什么?您确定库的公共头定义了类型吗?当然不是-API将其视为不透明类型,具有访问器函数,如

amqp\u get\u channel\u max()
@kingsmasher1:您是否包括私有头?(您可能不应该这样做,但是如果您真的想破坏接口并直接使用结构,那么您必须这样做)。