Ask Your Question

Why does this not compile? It is matching the documentation and other examples identically. Yet it gives me mismatch errors.

asked 2019-01-10 15:52:20 -0500

DevonW gravatar image

updated 2019-01-11 14:03:41 -0500

EDIT: I resolved the issue. I apologize I didn't give the full information to be able to solve it. The key missing info was the function wrapper that went around nh.subscribe. nh was defined as const in the function arg and therefore .subscribe cannot be called on a const node handle. This was resolved by adding:

ros::NodeHandle n(nh); so as to create a new node handle from the copied nodehandle passed in. Then, subscribe was eligible to be compiled.

Code which subscribes to topic topicName (from a method inside a class):

gps_sub = nh.subscribe("topicName", 1, &TestClass::testFunction, this);

I've tried putting it inside function definitions in the .cpp and .h files and neither work.

The function callback declaration:

void testFunction(const sensor_msgs::NavSatFix::ConstPtr& msg);

and definition:

void TestClass::testFuntion(const sensor_msgs::NavSatFix::ConstPtr& msg)
  UniqueLck lck(buffer_mtx);

What am I missing?

Edit: Adding in compiler error:

test_class.h:426:107: error: no matching function for call to ‘ros::NodeHandle::subscribe(const char [10], int, void (test_class::TestClass::*)(const ConstPtr&), test_class::TestClass*) const’
      gps_sub = nh.subscribe("topicName", 1, &TestClass::testFunction, this);

Edit: Adding the documentation/function it's supposed to match:

Which states:

Subscriber ros::NodeHandle::subscribe   (   const std::string &     topic,
uint32_t    queue_size,
void(T::*)(M) const     fp,
T *     obj,
const TransportHints &  transport_hints = TransportHints() 

In my above example, I have all of the arguments correct. The topic, the int value, the function pointer, and the "this" pointer to my object (since this is defined within the class).

Really pulling a blank here on why its not compiling.

Edit two: Adding the entire output log, with strings changed to protect confidentiality. (Just find and replace, shouldn't change any code)

In file included from /home/dash/work/company/code/codename/workspace/src/company/test_class/include/test_class/test_class_two.h:23:0,
                 from /home/dash/work/company/code/codename/workspace/src/company/test_class/src/test_class_two.cpp:13:
/home/dash/work/company/code/codename/workspace/src/company/test_class/include/test_class/test_class.h: In member function ‘bool test_class::TestClass::createSubscribers(const ros::NodeHandle&, const ros::NodeHandle&)’:
/home/dash/work/company/code/codename/workspace/src/company/test_class/include/test_class/test_class.h:426:80: error: no matching function for call to ‘ros::NodeHandle::subscribe(std::__cxx11::string, int, void (test_class::TestClass::*)(const ConstPtr&), test_class::TestClass*) const’
     gps_sub = nhp.subscribe(std::string("test"), 1, &TestClass::testFunction, this);
In file included from /opt/ros/kinetic/include/ros/ros.h:45:0,
                 from /home/dash/work/company/code/codename/workspace/src/company/test_class/include/test_class/test_class_two.h:18,
                 from /home/dash/work/company/code/codename/workspace/src/company/test_class/src/test_class_two.cpp:13:
/opt/ros/kinetic/include/ros/node_handle.h:402:14: note: candidate: ros::Subscriber ros::NodeHandle::subscribe(const string&, uint32_t, void (T::*)(M), T*, const ros::TransportHints&) [with M = const boost::shared_ptr<const sensor_msgs::NavSatFix_<std::allocator<void> > >&; T = test_class::TestClass; std::__cxx11::string = std::__cxx11::basic_string<char>; uint32_t = unsigned ...
edit retag flag offensive close merge delete



Please include the compiler error message in your question, too.

ahendrix gravatar image ahendrix  ( 2019-01-10 15:58:40 -0500 )edit

In addition to the error message, please include the documentation and other examples that you referred to.

jayess gravatar image jayess  ( 2019-01-10 16:05:00 -0500 )edit

I've edited the main post to fulfill both comment requests. Thank you both for helping!

DevonW gravatar image DevonW  ( 2019-01-10 16:38:30 -0500 )edit

This bit of the error message looks odd: void (test_class::TestClass::*)(const ConstPtr&), in particular, the function argument part of the signature that the compiler is returning to us is (const ConstPtr&), but I would expect it to be (const boost::shared_ptr<const sensor_msgs::NavSatFix>&)

ahendrix gravatar image ahendrix  ( 2019-01-11 01:24:01 -0500 )edit

Either you've aliased ConstPtr locally and the compiler is printing that (although I don't normally see gcc or clang do that) or it's somehow picking up a different function or a different definition of ConstPtr that doesn't match the template.

ahendrix gravatar image ahendrix  ( 2019-01-11 01:26:23 -0500 )edit

I would also normally expect gcc to emit all of the possible templates that could have matched; but that seems to be missing from the error message you've posted.

ahendrix gravatar image ahendrix  ( 2019-01-11 01:27:22 -0500 )edit

Mmm is void TestClass::testFuntion a typo in asking the question? It should be testFun c tion

aPonza gravatar image aPonza  ( 2019-01-11 08:39:04 -0500 )edit

@aPonza sorry that was a typo on my end putting it in. They were named differently, but due to confidentiality agreements the contents of this method may constitute a breach so I have renamed it for this post.

@ahendrix I will update with the full error (it is massive, mostly candidates)

DevonW gravatar image DevonW  ( 2019-01-11 10:21:48 -0500 )edit

3 Answers

Sort by » oldest newest most voted

answered 2019-01-11 14:09:07 -0500

ahendrix gravatar image

The key error message within all of those errors is:

/opt/ros/kinetic/include/ros/node_handle.h:402:14: note: passing ‘const ros::NodeHandle*’ as ‘this’ argument discards qualifiers

Your NodeHandle object in this context is const, and all of the variations of subscribe() are non-const. You need to update the surrounding code to pass in a non-const NodeHandle.

edit flag offensive delete link more


Yes this was the key error. It was vague in the sense that firstly it was a 'note' and not an error. But this fix definitely works. If anyone else has this error, you can remove the const from the nodehandle arg, or create a new nodehandle with the copy constructor.

DevonW gravatar image DevonW  ( 2019-01-11 15:06:48 -0500 )edit

answered 2019-01-10 17:06:47 -0500

mannyglover gravatar image

What if you convert the char[10] to a std::string before calling the function?

edit flag offensive delete link more


This unfortunately doesn't fix it.

DevonW gravatar image DevonW  ( 2019-01-11 10:22:08 -0500 )edit

However, looking more into this makes me think it is an issue between std::__stringcx11 and std::string. It appears that ROS Kinetic binaries are compiled using gcc 4.9, and my gcc is using 5.4 and c++11. However, I have my cmake as using c++1y. I don't think ROS' kinetic libraries support c11

DevonW gravatar image DevonW  ( 2019-01-11 11:56:50 -0500 )edit

answered 2019-01-10 20:36:03 -0500

Dyson sphere gravatar image

updated 2019-01-10 20:39:38 -0500

The problem is in type you provided as a first argument. You tried passing him the C style string which is as described in compiler error a const char[10]. What the function expects is a string from std library, std::string. If you look deeper into implementation of the ROS subscribe function (and available constructors) you will notice that he uses only this type of string and not C style strings.

This should work:

gps_sub = nh.subscribe(std::string("topicName"), 1, &TestClass::testFunction, this);
edit flag offensive delete link more


This does not fix the problem. I have used std::strings previously, and c++11 std strings and neither fixed the issue.

DevonW gravatar image DevonW  ( 2019-01-10 21:21:11 -0500 )edit

where is your nh.subscribe currently located? is it still in header or where? is it after you created nodehandle ? try putting both into constructor of your class.

Dyson sphere gravatar image Dyson sphere  ( 2019-01-10 21:45:18 -0500 )edit

I've tried it inside a function defined in a .cpp file and in the header file, the location doesn't seem to change anything. The nodehandle is passed in through a function argument if that matters.

DevonW gravatar image DevonW  ( 2019-01-11 10:22:57 -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



Asked: 2019-01-10 15:52:20 -0500

Seen: 450 times

Last updated: Jan 11 '19