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

Bool state is incorrect when retrieved from param server

asked 2014-09-29 07:32:12 -0600

GuoliangLiu gravatar image

updated 2020-08-25 23:10:21 -0600

M@t gravatar image

The code is:

ros::NodeHandle nh_private("~");     
bool state=false;

nh_private.param<bool>("state", state, "false");

It turns out the "state" is "true". Why???

edit retag flag offensive close merge delete



Following the answer from @kmhallen, this would seem to be expected (and correct) behaviour (you're just using the wrong type for your default value). Perhaps change your topic title to reflect this?

gvdhoorn gravatar image gvdhoorn  ( 2014-09-29 09:56:18 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2014-09-29 08:38:25 -0600

kmhallen gravatar image
template<typename T>
void param(const std::string& param_name, T& param_val, const T& default_val) const;

From the function prototype, param_val and default_val are both the template type, in this case bool.

The string value "false" is a pointer to the characters 'false' which is non-zero. This is interpreted as true when used as a bool. Try without quotes around false;

nh_private.param<bool>("state", state, false);
edit flag offensive delete link more


great! thinks!

GuoliangLiu gravatar image GuoliangLiu  ( 2014-10-07 20:25:47 -0600 )edit

I am getting the following error on ROS Noetic. Any ideas?

error: expected primary-expression before ‘bool’
   nh.getParam<bool>("dissemination_rules", dissemination_rules, true);
GPereira gravatar image GPereira  ( 2020-07-27 14:12:42 -0600 )edit

The getParam method does not have a default setting parameter (and does not require templating) like the param method.

So if you want to be able to set a default value then use the param method: nh.param<bool>("dissemination_rules", dissemination_rules, true);

Or if you want to just use getParam without setting a default value: nh.Param("dissemination_rules", dissemination_rules);

dwyackzan gravatar image dwyackzan  ( 2022-07-11 10:40:56 -0600 )edit

Question Tools

1 follower


Asked: 2014-09-29 07:32:12 -0600

Seen: 1,498 times

Last updated: Aug 25 '20