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

error: no matching function for call to advertiseService(). advertiseService is in constructor and the callback is a member function, attempting to use "this" as third argument to advertiseService()

asked 2021-05-28 15:53:35 -0500

chrism gravatar image

updated 2021-05-29 07:00:39 -0500

Hi there, this is my first time posting here so I appologize if don't layout my question in the best way.

I'm having an issue passing my callback function to the advertiseService() function. The error I get during the catkin_make process can be seen below:

error: no matching function for call to ‘ros::NodeHandle::advertiseService(const char [19], bool (KinovaPracticeServerClass::*)(package1::kinovaPracticeSrv::Request&, package1::kinovaPracticeSrv::Response&), KinovaPracticeServerClass*) const’

My package is supposed to work as follows: in my kinovaPractice.cpp file I have my main function where I initialize and then create the handle for a node. I then create a new object from a custom class named KinovaPracticeServerClass. This class has two functions, the first being the constructor and the second being the bool function which I want to pass as an argument to the advertiseservice() function.

a KinovaPracticeServerClass object is created in the main() function and the node handle from the main() function is passed to the constructor of the KinovaPracticeServerClass object. The constructor then tries to call the advertiseservice() member of the node handle passed to it. The callback is the bool function which is also in the class: KinovaPracticeServerClass. I have followed the convention in roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks but still am having issues.

What is interesting is that if I declare the callback above my main function in kinovaPractice.cpp and then advertise the service within the main function, the service works no problem.

I should also note that I am using a custom service I made called kinovaPracticeSrv.srv. The custom class I made is divided between a .h & .cpp file.

I am using ROS Melodic running Ubuntu 18.04 with the 5.4.0-73-generic kernel.

All files are contained within a package called "package1". All my code can be seen below.

kinovaPractice.cpp:

#include<iostream>
#include"ros/ros.h"
#include"kinovaPracticeClass.h"
#include"std_msgs/String.h"
#include<functional>
#include "package1/kinovaPracticeSrv.h"
#include "kinovaPracticeServerClass.h"

//UNCOMMENTING THIS FUNCTION BELOW ALONG WITH THE OTHER LINE IN THE MAIN() WILL ALLOW FOR MY SERVICE TO WORK PROPERLY
/*
bool serverCallback(package1::kinovaPracticeSrv::Request &req,package1::kinovaPracticeSrv::Response &res)
{
  //going to print the joint angles that were received
  std::cout<<"Received the following for joint1: "<<req.joint1<<std::endl;
  std::cout<<"Received the following for joint2: "<<req.joint2<<std::endl;
  std::cout<<"Received the following for joint3: "<<req.joint3<<std::endl;
  std::cout<<"Received the following for joint4: "<<req.joint4<<std::endl;
  std::cout<<"Received the following for joint5: "<<req.joint5<<std::endl;
  std::cout<<"Received the following for joint6: "<<req.joint6<<std::endl;
  return true;
}//End of the server callback
*/

int main(int argc, char **argv)
{

//initilize node and create node handle
ros::init(argc, argv, "kinovaPracticeNode");
ros::NodeHandle nh("~");//This should make this node handle avalible to all other namespaces?
//ros::ServiceServer server=nh.advertiseService("MoveToHomePosition",serverCallback);<--IF THIS LINE IN UNCOMMENTED ALONG WITH THE FUNCTION ABOVE, THE CALLBACK WORKS FINE

//Creating a kinovaPractceServerClass Object which will initilize my custom services in it's constructor
KinovaPracticeServerClass customServerObj(nh);

while(ros ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2021-05-29 08:48:08 -0500

chrism gravatar image

After some more digging I found the solution from a similair question asked earlier Why does this not compile? It is matching the documentation and other examples identically. Yet it gives me mismatch errors..

The problem was that I was passing a constant node handle into my constructor and then trying to advertise a service which wasn't allow. The solution, mentioned in the link above, was to remove the const from the node handle argument. Another solution I found would be to have a private member of type ros::NodeHandle and then declare it through a memeber initializer.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-05-28 15:53:35 -0500

Seen: 979 times

Last updated: Jun 15 '21