0
$\begingroup$

Rosanswers logo

Hello. I am trying to use RCLCPP_XX_STREAM function passing directly a message to the stream. The problem is that I am getting compilation errors because the message ostream operators are not found.

Below I show an example:

#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
[...]
geometry_msgs::msg::PoseStamped poseMsg = [...];
RCLCPP_INFO_STREAM(get_logger(), "posevalue:" << poseMsg);
[...]

Error:

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘geometry_msgs::msg::PoseStamped_std::allocator<void >::pose_type’ {aka ‘geometry_msgs::msg::Posestd::allocator<void >’})

Where are rclcpp message ostream operators for logging?

Are they available in ROS2? if not, is it planned to support them?


Originally posted by Pablo Iñigo Blasco on ROS Answers with karma: 2982 on 2020-10-29

Post score: 5

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

UPDATE: It seems the original method below is now deprecated in Humble. You should now use geometry_msgs::msg::to_yaml() instead of rosidl_generator_traits::to_yaml()

warning: ‘std::string rosidl_generator_traits::to_yaml(const Pose&)’ is deprecated: use 
geometry_msgs::msg::to_yaml() instead [-Wdeprecated-declarations]

So, the answer for Humble is

#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
[...]
geometry_msgs::msg::PoseStamped poseMsg = [...];
RCLCPP_INFO_STREAM(get_logger(), "posevalue:" << geometry_msgs::msg::(poseMsg));
[...]

ORIGINAL: I had the same issue... here's how to fix it:
use rosidl_generator_traits::to_yaml(poseMsg) instead of just poseMsg

#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/pose_stamped.hpp>
[...]
geometry_msgs::msg::PoseStamped poseMsg = [...];
RCLCPP_INFO_STREAM(get_logger(), "posevalue:" << rosidl_generator_traits::to_yaml(poseMsg));
[...]

I dug through the generated message header files and found it.


Originally posted by m.anderson with karma: 91 on 2022-02-17

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.