简体   繁体   中英

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

I'm using Kinetic.

I have a custom message path.msg

string path_name
segment[] segments

I'm trying to send a ROS goal with that message type.

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 is a std::vector<segment> , but your segments variable is just a single segment, not a vector of segments. If you want to add only one segment, you can use action_path.push_back(segment) . Otherwise, you can declare segments as

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

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

See How to initialize std::vector from C-style array? for more about setting a vector from a raw C-array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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