c++ - 如何在 C++ 中正确地从 ROS 发送数组?
问题描述
我正在使用动力学。
我有一条自定义消息path.msg
string path_name
segment[] segments
我正在尝试使用该消息类型发送 ROS 目标。
我在我的代码中初始化了一个数组
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
我收到这个错误
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;
我假设action_path.segments
采用不同的数据类型,但从该错误消息中我不明白该数据类型是什么。
解决方案
action_path.segments
是 a std::vector<segment>
,但您的segments
变量只是一个段,而不是段向量。如果您只想添加一个段,您可以使用action_path.push_back(segment)
. 否则,您可以声明segments
为
std::vector<cuarl_rover_planner::segment> segments(path->getSegments().size());
如果您出于某种原因想使用原始指针数组(就像您可能在这里),您必须首先明确设置它std::vector
,即
action_path.segments = std::vector<cuarl_rover_planner::segment>(segments, segments+path->getSegments().size());
请参阅如何从 C 样式数组初始化 std::vector?有关从原始 C 数组设置向量的更多信息。
推荐阅读
- prolog - 遍历整个列表并检查 Prolog 中是否存在项目
- laravel - 根据最后一个元素的 where 条件获取所有父元素
- html - Navbar Hover DropDown Expand - Angular
- nuxt.js - 是否可以在没有“build”的情况下在“target:static”模式下使用“nuxt generate”命令更新数据(来自 API)?
- wkhtmltopdf - 使用 Wkhtmltopdf 打印制表符(错误:警告:未定义:0 类型错误:'[object EventConstructor]' is not a constructor)
- visual-studio-code - VS-Code 远程 SSH:无法设置套接字 - 其他客户端还可以吗?
- node.js - 如何在与nodejs服务器的本机反应中实现expo推送通知
- android - 如何在应用程序的另一部分访问由 onCreatePreferences() 创建的首选项?
- javascript - 如何在聊天应用程序中在另一条消息下方显示一条消息?
- algorithm - 覆盖所有路线的最短路径算法