gpt4 book ai didi

c++ - 使用消息在 ROS 上发送 C 数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:33:03 24 4
gpt4 key购买 nike

我的 ROS 消息很简单:

int8[64] packet1

我在我的谈话节点中发布:

terp::Packet1 msg;
msg.packet1={0,1,0,1,0,1};
ROS_INFO("Packet in string form: %s", msg->packet1);
chatter_pub.publish(msg);
ros::spinOnce();

我在我的监听节点中检索:

void resolve_input1(const uint8_t msg[]) {
if (sizeof(msg->packet1)/sizeof(msg->packet1[0])<MAX_MSG_LEN) {
memcpy(msg1,msg->packet1);
ROS_INFO("I heard: [%s]\n",msg1);
} else
ROS_ERROR("Message from node 1 too long");
}

但是,在制作项目时,我收到以下错误:

listener.cpp:16:19: error: request for member 'packet1' in '*msg',
which is of non-class type 'const uint8_t {aka const unsigned char}
if (sizeof(msg->packet1)/sizeof(msg->packet1[0])<MAX_MSG_LEN) {

将 resolve_input1 的声明更改为:

void resolve_input1(const terp::Packet1 msg) {

编辑:摆脱了那个错误,但是现在出现了一个新的错误,这是相似的;强调我对消息的困惑:

listener.cpp:16:17 error: base operand of '->' has non-pointer type
'const Packet1 {aka const terp::Packet1_<std::allocator<void> >}'
if (sizeof(msg->packet1)/sizeof(msg->packet1[0])<MAX_MSG_LEN) {
^

最佳答案

resolve_input1 中,msgconst uint8_t* 类型,因此您可能必须将其reinterpret_castconst terp::Packet1* 在访问字段之前。

更新:根据您的编辑,只需将 msg-> 更改为 msg.

关于c++ - 使用消息在 ROS 上发送 C 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34668623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com