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

Getting error: ‘operator()’ is not a member of ‘void while creating a service

asked 2023-03-13 10:43:39 -0500

IvanTomaev gravatar image

I have a service, that takes encoded data and returns decoded. in package quadro_interface i have implementation for strings (Coder.srv, Decoder.srv) and for byte arrays (BinaryCoder.srv and BinaryDecoder.srv). I am trying to create a c++ server for BinaryDecoder, but I get this error every time I try to build it. Server code and error message below. Code:

#include "rclcpp/rclcpp.hpp"
#include "quadro_interface/srv/binary_decoder.hpp"   
#include "std_msgs/msg/string.hpp"
#include <memory>
#include <string>
#include <vector>

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
using std::string;
class Decoder : public rclcpp::Node{



public:
    Decoder()
    : Node("Decoder"){
         // CHANGE
      rclcpp::Service<quadro_interface::srv::BinaryDecoder>::SharedPtr service = 
      this->create_service<quadro_interface::srv::BinaryDecoder>("binary_decoder",  &Decoder::decode);
      subscriber_ = this->create_subscription<std_msgs::msg::String>("key_topic", 10, std::bind(&Decoder::topic_callback, this, _1));

    }

  private:
    string key_;
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscriber_;
    size_t count_;
    void decode(std::shared_ptr<quadro_interface::srv::BinaryDecoder::Request> request,std::shared_ptr<quadro_interface::srv::BinaryDecoder::Response>       response){
          std::vector<unsigned char> data = request -> encoded;
          unsigned char temp;
          for(int i = 0; i < data.size()-1; i++){
              if(i%2 == 0){
                temp = data[i];
                data[i] = data[i+1];
                data[i+1] = temp;
              }

              }
          response->data = {1,2,3,4};
                   }

    void topic_callback(const std_msgs::msg::String & msg)
    {
      key_ = msg.data;
    }

};

Error Message:

      --- stderr: quadro_ext                             
        /home/computer/ros2_ws/src/quadro_ext/src/decoder.cpp: In member function ‘void Decoder::decode(std::shared_ptr<quadro_interface::srv::BinaryDecoder_Request_<std::allocator<void> > >, std::shared_ptr<quadro_interface::srv::BinaryDecoder_Response_<std::allocator<void> > >)’:
        /home/computer/ros2_ws/src/quadro_ext/src/decoder.cpp:33:28: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<unsigned char, std::allocator<unsigned char> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
           33 |           for(int i = 0; i < data.size()-1; i++){
              |                          ~~^~~~~~~~~~~~~~~
        In file included from /opt/ros/humble/include/rclcpp/rclcpp/client.hpp:39,
                         from /opt/ros/humble/include/rclcpp/rclcpp/callback_group.hpp:24,
                         from /opt/ros/humble/include/rclcpp/rclcpp/any_executable.hpp:20,
                         from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategy.hpp:25,
                         from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategies.hpp:18,
                         from /opt/ros/humble/i

    nclude/rclcpp/rclcpp/executor_options.hpp:20,
                     from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:37,
                     from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25,
                     from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21,
                     from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155,
                     from /home/computer/ros2_ws/src/quadro_ext/src/decoder.cpp:1:
    /opt/ros/humble/include/rclcpp/rclcpp/function_traits.hpp: In instantiation of ‘struct rclcpp::function_traits::function_traits<void (Decoder::*)(std::shared_ptr<quadro_interface::srv::BinaryDecoder_Request_<std::allocator<void> > >, std::shared_ptr<quadro_interface::srv::BinaryDecoder_Response_<std::allocator<void> > >)>’:
    /opt/ros/humble/include/rclcpp/rclcpp/function_traits.hpp:165:8:   required from ‘struct rclcpp::function_traits::same_arguments<void (Decoder::*)(std::shared_ptr<quadro_interface::srv::BinaryDecoder_Request_<std::allocator<void> > >, std::shared_ptr<quadro_interface::srv::BinaryDecoder_Response_<std::allocator<void> > >), std::function<void(std::shared_ptr<quadro_interface::srv::BinaryDecoder_Request_<std::allocator<void> > >, std::shared_ptr<quadro_interface::srv::BinaryDecoder_Response_<std::allocator<void> > >)> >’
    /opt/ros/humble/include/rclcpp/rclcpp/any_service_callback.hpp:121:10:   required from ‘void rclcpp::AnyServiceCallback<ServiceT>::set(CallbackT&&) [with CallbackT = void (Decoder::*)(std ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-03-20 16:54:37 -0500

jdlangs gravatar image

The create_service function expects a pointer to a callback that takes two arguments: request and response. But you're passing a member function pointer, which takes three because of the implicit this pointer argument. You can either use a lambda function or std::bind to create a new function object that takes two arguments, similar to what is done with the create_subscription call in the next line.

edit flag offensive delete link more

Comments

Thak you for your responce

IvanTomaev gravatar image IvanTomaev  ( 2023-04-05 06:06:05 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2023-03-13 10:43:39 -0500

Seen: 386 times

Last updated: Mar 20 '23