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

时间:2016-01-08 02:38:48

标签: c++ c pointers ros

我的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) {
                  ^

1 个答案:

答案 0 :(得分:1)

resolve_input1 msg类型为const uint8_t*,因此您在访问字段之前可能需要reinterpret_castconst terp::Packet1*

<强>更新: 根据您的修改,只需将msg->更改为msg.