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

no matching function for call to

asked 2018-02-23 10:20:17 -0500

Pigskin gravatar image

Hey everybody, I'm working on a node that uses a service to repeat a string a variable amount of times. for this i made the following srv file:

string text
int32 nrOfDuplicates
---

this will be processed by the following C++ code:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "std_msgs/Int32.h"
#include <string>

void multiply (std_msgs::String::ConstPtr   & str, std_msgs::Int32      & var){
int amount = var.data;
    for(int i = 0 ; i < amount ; i++)
    {
        ROS_INFO("%s", str->data.c_str());
    }
 ROS_INFO("/n");
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "add_duplicates");
  ros::NodeHandle n;
  ros::ServiceServer service = n.advertiseService("add_duplicates", multiply);
  ros::spin();

  return 0;
}

however, when i use catkin_make to compile the node, i get the following error:

/home/osboxes/catkin_ws/src/final/src/duplicator.cpp: In function ‘int main(int, char**)’:
/home/osboxes/catkin_ws/src/final/src/duplicator.cpp:19:77: error: no matching function for call to ‘ros::NodeHandle::advertiseService(const char [15], void (&)(std_msgs::String_<std::allocator<void> >::ConstPtr&, std_msgs::Int32&))’
   ros::ServiceServer service = n.advertiseService("add_duplicates", multiply);
                                                                             ^

I was considering that it is looking for a function that uses a string and int32 instead of the Std_msgs counterpart, but i'm a total ROS newbie and am not sure if this is true, or how to write it otherwise.

all help is appreciated!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2018-02-23 10:28:04 -0500

gvdhoorn gravatar image

Your multiply callback is not compatible with what ServiceServer can use. That is why you get the error.

i'm a total ROS newbie and am not sure if this is true, or how to write it otherwise.

We are more than willing to help you here on ROS Answers, but just to make sure (and to get some very basic stuff out of the way): have you done the Writing a Simple Service and Client (C++) tutorial?

That should show you how to write a proper service server callback.

See also wiki/roscpp/Overview/Services, specifically: wiki/roscpp/Overview/Services - Providing Services - Callback Signature.

edit flag offensive delete link more

Comments

I have, However I think i lost the important bits when i got confused when they imported the srv from a different package. I have a C background and this is my first week using C++, so all constructor systems is still very disorientating. The last 2 links i found very useful.

Pigskin gravatar image Pigskin  ( 2018-02-23 10:44:53 -0500 )edit

However, I still have a few questions. I have distilled that the callback should be a boolean, so it can check if the function worked, and uses 2 variables, one to import, one to return. but what type must i use for the import? i have both a std_msgs::string and std_msgs::int32. empty?

Pigskin gravatar image Pigskin  ( 2018-02-23 10:47:12 -0500 )edit

Can you please check the tutorial I linked, specifically the second paragraph in the The Code Explained section of the server? That should explain what you need to make this work. Service server callbacks only take two args: a request and a reply object. Not fields of the msg.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-23 11:02:09 -0500 )edit

I have a C background and this is my first week using C++, so all constructor systems is still very disorientating.

it's up to you, but learning two new things at the same time could be more confusing than it's worth. It might be beneficial to do a few C++ tutorials first, then come back to ROS.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-23 11:02:54 -0500 )edit

Question Tools

Stats

Asked: 2018-02-23 10:20:17 -0500

Seen: 3,810 times

Last updated: Feb 23 '18