简体   繁体   English

如何在 C++ 中正确地从 ROS 发送数组?

[英]How do you properly send an array from ROS in C++?

I'm using Kinetic.我正在使用动力学。

I have a custom message path.msg我有一个自定义消息path.msg

string path_name
segment[] segments

I'm trying to send a ROS goal with that message type.我正在尝试使用该消息类型发送 ROS 目标。

I initialize an array in my code我在我的代码中初始化了一个数组

cuarl_rover_planner::segment segments[path->getSegments().size()];
//do stuff that populates the array
cuarl_rover_planner::path action_path;
action_path.segments = segments; // Error here

I get this error我收到此错误

error: no match for ‘operator=’ (operand types are ‘cuarl_rover_planner::path_<std::allocator<void> >::_segments_type {aka std::vector<cuarl_rover_planner::segment_<std::allocator<void> >, std::allocator<cuarl_rover_planner::segment_<std::allocator<void> > > >}’ and ‘cuarl_rover_planner::segment [(<anonymous> + 1)] {aka cuarl_rover_planner::segment_<std::allocator<void> > [(<anonymous> + 1)]}’)
         action_path.segments = segments;

I'm assuming that the action_path.segments take a different data type, but I don't understand what that datatype is, from that error message.我假设action_path.segments采用不同的数据类型,但从该错误消息中我不明白该数据类型是什么。

action_path.segments is a std::vector<segment> , but your segments variable is just a single segment, not a vector of segments. action_path.segments是一个std::vector<segment> ,但你的segments变量只是一个段,而不是一个段向量。 If you want to add only one segment, you can use action_path.push_back(segment) .如果只想添加一个段,可以使用action_path.push_back(segment) Otherwise, you can declare segments as否则,您可以将segments声明为

std::vector<cuarl_rover_planner::segment> segments(path->getSegments().size());

If you wanted to use a raw pointer array for some reason (like you might be here), you have to explicitly set that up as std::vector first, ie如果您出于某种原因想使用原始指针数组(就像您可能在这里),您必须首先将其明确设置为std::vector ,即

action_path.segments = std::vector<cuarl_rover_planner::segment>(segments, segments+path->getSegments().size());

See How to initialize std::vector from C-style array?请参阅如何从 C 样式数组初始化 std::vector? for more about setting a vector from a raw C-array.有关从原始 C 数组设置向量的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM