ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

publishing int variable

asked 2017-02-13 03:57:59 -0600

fafa gravatar image

updated 2017-02-13 04:31:57 -0600


I want to publish a variable, called targetIdx (type int), but I always get an error message

That's an extract of my publisher:

std_msgs::Int32 targetIdx; = targetIdx;
ROS_INFO("Sensor Target Nr: %d",;

That's the error message:

cannot convert ‘std_msgs::Int32’ to ‘std_msgs::Int32_<std::allocator<void> >::_data_type {aka int}’ in assignment = targetIdx;

How can I solve the problem?


I have declared the variable targetIdx as type int in the beginning of my program.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-02-13 04:10:35 -0600

gvdhoorn gravatar image

updated 2017-02-13 04:11:06 -0600

std_msgs::Int32 targetIdx;

So targetIdx is declared to be of type std_msgs::Int32 here .. = targetIdx;

.. and here you assign the just instantiated msg to its own data member.

That is not going to work.

Could it be that you are shadowing your int targetIdx with the newly created std_msgs::Int32 targetIdx?

edit flag offensive delete link more


Sorry I'm new to programming and ROS...could you please give me an example how to publish an int variable

fafa gravatar image fafa  ( 2017-02-13 04:37:21 -0600 )edit

This is a C++ issue really, not a ROS issue.

You are creating a two variables with the same name. The compiler will only 'remember' the one that you created last. In this case that is the std_msgs::Int32 one, not the int.

gvdhoorn gravatar image gvdhoorn  ( 2017-02-13 05:37:23 -0600 )edit

Ok. The targetIdx variable is delivered by a sensor. So how can I easily publish this delivered variable?

fafa gravatar image fafa  ( 2017-02-13 05:45:29 -0600 )edit

Just give your std_msgs::Int32 instance a different name. Then everyhing should start working (provided you have the rest correctly implemented).

gvdhoorn gravatar image gvdhoorn  ( 2017-02-15 05:57:07 -0600 )edit

Question Tools


Asked: 2017-02-13 03:57:59 -0600

Seen: 1,117 times

Last updated: Feb 13 '17