为什么我在打印这个结构指针时出错
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 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( );
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- C++:映射结构中的结构指针
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- 类的静态结构指针声明在C++
- 在两个.cpp文件之间定义全局类/结构指针
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 是否可以将无符号字符数组reinterpret_cast到仅包含C++中无符号字符成员的结构指针
- 如何在地图中使用结构指针
- 结构指针在"新"调用上给出已分配的地址?
- 铸造结构指针指向C 的工会指针
- c++ 结构指针在初始化为 NULL 时无法读取内存
- 函数中的结构指针的地址为0x1
- C 用结构指针初始化结构
- C 结构指针铸成数组Interop C#
- 方法中不允许访问结构指针
- 如何打印出结构指针
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- 尝试将结构指针传递给类时出错
- 初始化结构指针的值
- 使用 pybind11 将自定义结构(指针向量)从 C++ 传递到 python 的方法是什么?