Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.
This repository was archived by the owner on May 31, 2025. It is now read-only.

ros::Publisher::publish doesn't accept basic types for std_msgs  #1972

@brawner

Description

@brawner

I am having trouble compiling a simple roscpp publisher that publishes raw doubles on a topic of std_msgs::Float64. In Melodic, you could publish the raw data value straight. Was this functionality intended to be changed?

Installed from Noetic binaries, with an upgraded workspace from previous distributions. The following example fails to compile for me. I could use help confirming this is not just in my head.

ros::Publisher publisher = nh.advertise<std_msgs::Float64>("numbers", 1);
double d = 1.0;
publisher.publish(d);

Whereas, the following works:

std_msgs::Float64 m;
m.data = 1.0;
publisher.publish(m);

Full example

#include <random>

#include <ros/ros.h>
#include <std_msgs/Float64.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "publisher");
  ros::NodeHandle nh;
  ros::Publisher publisher = nh.advertise<std_msgs::Float64>("numbers", 1);
  ros::Rate publish_rate(10);

  while (ros::ok()) {
    double d = 1.0;
    publisher.publish(d);
    publish_rate.sleep();
  }
}
In file included from /opt/ros/noetic/include/ros/serialization.h:37,
                 from /opt/ros/noetic/include/ros/publisher.h:34,
                 from /opt/ros/noetic/include/ros/node_handle.h:32,
                 from /opt/ros/noetic/include/ros/ros.h:45,
                 from /home/brawner/catkin_ws/src/myrobot/examples/src/example.cpp:3:
/opt/ros/noetic/include/ros/message_traits.h: In instantiation of ‘static const char* ros::message_traits::MD5Sum<M>::value(const M&) [with M = int]’:
/opt/ros/noetic/include/ros/message_traits.h:254:102:   required from ‘const char* ros::message_traits::md5sum(const M&) [with M = int]’
/opt/ros/noetic/include/ros/publisher.h:114:7:   required from ‘void ros::Publisher::publish(const M&) const [with M = int]’
/home/brawner/catkin_ws/src/myrobot/examples/src/example.cpp:21:24:   required from here
/opt/ros/noetic/include/ros/message_traits.h:125:14: error: request for member ‘__getMD5Sum’ in ‘m’, which is of non-class type ‘const int’
  125 |     return m.__getMD5Sum().c_str();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions