ROS_INFO_STREAM lack of operator overload?

asked 2021-05-27 03:35:32 -0600

gyrados10 gravatar image

I am writing a callback function which subscribes to multiple topics. In the callback, i am unable to print console certain structs and it is puzzling. Shouldn't the operators be overloaded already? I am getting the error

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const _data_type {aka const std::vector<unsigned char="">}’)

static void callback(const sensor_msgs::PointCloud2ConstPtr& sub1_ptr,const sensor_msgs::PointCloud2ConstPtr& sub2_ptr, const sensor_msgs::PointCloud2ConstPtr& sub3_ptr, const sensor_msgs::PointCloud2ConstPtr& sub4_ptr){

ROS_INFO_STREAM("1st"<< sub1_ptr->data);
ROS_INFO_STREAM("2nd"<< sub2_ptr->data);
ROS_INFO_STREAM("3rd"<< sub3_ptr);
ROS_INFO_STREAM("4th"<< sub4_ptr);                                                                     }

So basically a pointer (just an address) can be printed, no surprise there. But why can't i print something like a PointCloud2.data? And also, ROS_INFO is the c version of the ros print console function but would it be easier to print something compared to ROS_INFO_STREAM?

edit retag flag offensive close merge delete

Comments

C++ doesn't have any overload for << for std::vector<T> by default. That's not ROS specific, but C++.

What made you assume such an overload should be provided?

gvdhoorn gravatar image gvdhoorn  ( 2021-05-27 05:43:11 -0600 )edit

It feels right for ROS to provide certain functionalities to go along with ROS_INFO_STREAM. After all, the things we will print to console would likely be visualisation of ROS data, especially the data structs. It is like coming up with a new coffee machine that makes a new type of coffee but you have to make the gears inside yourself. Anyways, i could write the overload myself for sure but that would assume i understand the data and a quick way to understand the data would be to print it out to console but i cannot do that in such a case. So yeah.

gyrados10 gravatar image gyrados10  ( 2021-05-27 05:58:54 -0600 )edit

It's probably pedantic, but a std::vector<unsigned char> is not "a ROS data struct". It's a plain C++ STL type.

There are typically overloads for ROS message types, but that's not what you're passing to ROS_INFO_STREAM(..) in your example.

i could write the overload myself for sure but that would assume i understand the data

According to sensor_msgs/PointCloud2, the data field is a uint8[], which is mapped to a std::vector<unsigned char>. std::to_string(..) still doesn't support that, so you'd have to implement that yourself, yes.

gvdhoorn gravatar image gvdhoorn  ( 2021-05-27 06:04:41 -0600 )edit