[英]Why do I get error in printing this structure pointer?
我的庫(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類型是不透明的句柄
因此,您的帖子似乎並不完全真實, struct 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( );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.