Ask Your Question

Error in template instantiation for ros::NodeHandle::param [closed]

asked 2018-12-05 05:36:31 -0600

aPonza gravatar image

updated 2018-12-05 05:40:30 -0600

I'm trying to get arm_id from the parameter server using function template<typename T> T param(const std::string& param_name, const T& default_val) so as to initialize the variable in the initializer list. However, it seems the wrong template is getting picked for resolution. This is a MWCE (you would probably need to set some parameter first via launch file for this to actually work and not only compile):

class Foo

  static constexpr auto default_arm_id = "foo";
  ros::NodeHandle nh_;
  std::string arm_id_;

    : nh_ {}
    , arm_id_ {nh_.param("/franka_state_controller/arm_id", default_arm_id)}

and the error I get is:

In file included from /home/(...)/foo.h:16:0,
                 from /home/(...)/foo.cpp:10:
/opt/ros/kinetic/include/ros/node_handle.h: In instantiation of ‘bool ros::NodeHandle::param(const string&, T&, const T&) const [with T = const char*; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
/opt/ros/kinetic/include/ros/node_handle.h:2156:7:   required from ‘T ros::NodeHandle::param(const string&, const T&) [with T = const char*; std::__cxx11::string = std::__cxx11::basic_string<char>]’
/home/(...)/foo.cpp:56:72:   required from here
/opt/ros/kinetic/include/ros/node_handle.h:2124:7: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
       if (getParam(param_name, param_val))
/opt/ros/kinetic/include/ros/node_handle.h:1705:8: note:   initializing argument 2 of ‘bool ros::NodeHandle::getParam(const string&, int&) const’
   bool getParam(const std::string& key, int& i) const;
/opt/ros/kinetic/include/ros/node_handle.h:2124:7: error: cannot bind rvalue ‘(int)((long int)param_val)’ to ‘int&’
       if (getParam(param_name, param_val))
make[2]: *** [CMakeFiles/foo.dir/src/foo.cpp.o] Error 1
make[1]: *** [CMakeFiles/foo.dir/all] Error 2
make: *** [all] Error 2

The same happens if I initialize in the body of the constructor, by the way.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by aPonza
close date 2018-12-18 05:42:57.328844

1 Answer

Sort by » oldest newest most voted

answered 2018-12-05 06:07:25 -0600

Try explictly specifying the template type when calling the function as below.

    : nh_ {}
    , arm_id_ {nh_.param<std::string>("/franka_state_controller/arm_id", default_arm_id)}

With the auto declaration of the default value I don't think the compiler is able to resolve the type for the parameter. let us know if this fixes it.

edit flag offensive delete link more



Yes, absolutely had not thought of that. Thanks!

aPonza gravatar image aPonza  ( 2018-12-05 06:18:42 -0600 )edit

Question Tools

1 follower


Asked: 2018-12-05 05:36:31 -0600

Seen: 617 times

Last updated: Dec 05 '18