Why does this not compile? It is matching the documentation and other examples identically. Yet it gives me mismatch errors.
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);
gps_buff.push_back(msg);
cv.notify_one();
}
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:
http://docs.ros.org/lunar/api/roscpp/...
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 ...
Please include the compiler error message in your question, too.
In addition to the error message, please include the documentation and other examples that you referred to.
I've edited the main post to fulfill both comment requests. Thank you both for helping!
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>&)
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.
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.
Mmm is
void TestClass::testFuntion
a typo in asking the question? It should be testFun c tion@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)