Ask Your Question
0

Bool state is incorrect when retrieved from param server

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

GuoliangLiu gravatar image

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

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

Comments

1

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 -0500 )edit

1 Answer

Sort by » oldest newest most voted
3

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

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

Comments

great! thinks!

GuoliangLiu gravatar image GuoliangLiu  ( 2014-10-07 20:25:47 -0500 )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 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 1,173 times

Last updated: Aug 25 '20