0
$\begingroup$

Rosanswers logo

Hi people! I'm starting with ROS and I have a problem implementing a client and server. I used the files created in the tutorials of ROS but I would like to return an array of strings and I don't know how to do it. These are my codes:

get_element_server.cpp:

#include "ros/ros.h"
#include "beginner_tutorials/GetElement.h"

bool add(beginner_tutorials::GetElement::Request  &req,
         beginner_tutorials::GetElement::Response &res)
{
  res.result = {"table", "chair"};
  ROS_INFO("request: x=%s", req.name.c_str());
  ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
  return true;
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "get_element_server");
  ros::NodeHandle n;

  ros::ServiceServer service = n.advertiseService("get_element", add);
  ROS_INFO("Ready to get element.");
  ros::spin();

  return 0;
}

get_element_client.cpp:

#include "ros/ros.h"
#include "beginner_tutorials/GetElement.h"
#include <cstdlib>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "get_element_client");
  if (argc != 2)
  {
    ROS_INFO("usage: get_element_client name");
    return 1;
  }

  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<beginner_tutorials::GetElement>("get_element");
  beginner_tutorials::GetElement srv;
  srv.request.name = argv[1];
  if (client.call(srv))
  {
    ROS_INFO("Elements: %s, %s", srv.response.result[0].c_str(), srv.response.result[1].c_str());
  }
  else
  {
    ROS_ERROR("Failed to call service get_element");
    return 1;
  }

  return 0;
}

GetElement.h

string name
---
string[] result

The error that I obtain is the following when I use 'catkin_make':

/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp: In function ‘bool add(beginner_tutorials::GetElement::Request&, beginner_tutorials::GetElement::Response&)’:
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:7:7: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
   res.result = {"table", "chair"};
       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:7:33: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
   res.result = {"table", "chair"};
                                 ^
In file included from /opt/ros/indigo/include/ros/ros.h:40:0,
                 from /home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:1:
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:8:33: error: ‘beginner_tutorials::GetElement::Request’ has no member named ‘name’
   ROS_INFO("request: x=%s", req.name.c_str());
                                 ^
/opt/ros/indigo/include/ros/console.h:342:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’
     ::ros::console::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
                                                                                                                                                                     ^
/opt/ros/indigo/include/ros/console.h:375:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’
       ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
       ^
/opt/ros/indigo/include/ros/console.h:557:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^
/opt/ros/indigo/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’
 #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:8:3: note: in expansion of macro ‘ROS_INFO’
   ROS_INFO("request: x=%s", req.name.c_str());
   ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:9:52: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
   ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
                                                    ^
/opt/ros/indigo/include/ros/console.h:342:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’
     ::ros::console::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
                                                                                                                                                                     ^
/opt/ros/indigo/include/ros/console.h:375:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’
       ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
       ^
/opt/ros/indigo/include/ros/console.h:557:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^
/opt/ros/indigo/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’
 #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:9:3: note: in expansion of macro ‘ROS_INFO’
   ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
   ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:9:75: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
   ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
                                                                           ^
/opt/ros/indigo/include/ros/console.h:342:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’
     ::ros::console::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
                                                                                                                                                                     ^
/opt/ros/indigo/include/ros/console.h:375:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’
       ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
       ^
/opt/ros/indigo/include/ros/console.h:557:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^
/opt/ros/indigo/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’
 #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:9:3: note: in expansion of macro ‘ROS_INFO’
   ROS_INFO("sending back response: [%s] [%s]", res.result[0].c_str(), res.result[1].c_str());
   ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp: In function ‘int main(int, char**)’:
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp:17:15: error: ‘beginner_tutorials::GetElement::Request’ has no member named ‘name’
   srv.request.name = argv[1];
               ^
In file included from /opt/ros/indigo/include/ros/ros.h:40:0,
                 from /home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp:1:
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp:20:47: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
     ROS_INFO("Elements: %s, %s", srv.response.result[0].c_str(), srv.response.result[1].c_str());
                                               ^
/opt/ros/indigo/include/ros/console.h:342:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’
     ::ros::console::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
                                                                                                                                                                     ^
/opt/ros/indigo/include/ros/console.h:375:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’
       ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
       ^
/opt/ros/indigo/include/ros/console.h:557:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^
/opt/ros/indigo/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’
 #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp:20:5: note: in expansion of macro ‘ROS_INFO’
     ROS_INFO("Elements: %s, %s", srv.response.result[0].c_str(), srv.response.result[1].c_str());
     ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp:20:79: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’
     ROS_INFO("Elements: %s, %s", srv.response.result[0].c_str(), srv.response.result[1].c_str());
                                                                               ^
/opt/ros/indigo/include/ros/console.h:342:165: note: in definition of macro ‘ROSCONSOLE_PRINT_AT_LOCATION_WITH_FILTER’
     ::ros::console::print(filter, __rosconsole_define_location__loc.logger_, __rosconsole_define_location__loc.level_, __FILE__, __LINE__, __ROSCONSOLE_FUNCTION__, __VA_ARGS__)
                                                                                                                                                                     ^
/opt/ros/indigo/include/ros/console.h:375:7: note: in expansion of macro ‘ROSCONSOLE_PRINT_AT_LOCATION’
       ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
       ^
/opt/ros/indigo/include/ros/console.h:557:35: note: in expansion of macro ‘ROS_LOG_COND’
 #define ROS_LOG(level, name, ...) ROS_LOG_COND(true, level, name, __VA_ARGS__)
                                   ^
/opt/ros/indigo/include/rosconsole/macros_generated.h:110:23: note: in expansion of macro ‘ROS_LOG’
 #define ROS_INFO(...) ROS_LOG(::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
                       ^
/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_client.cpp:20:5: note: in expansion of macro ‘ROS_INFO’
     ROS_INFO("Elements: %s, %s", srv.response.result[0].c_str(), srv.response.result[1].c_str());
     ^
make[2]: *** [beginner_tutorials/CMakeFiles/get_element_server.dir/src/get_element_server.cpp.o] Error 1
make[1]: *** [beginner_tutorials/CMakeFiles/get_element_server.dir/all] Error 2
make[1]: *** Se espera a que terminen otras tareas....
make[2]: *** [beginner_tutorials/CMakeFiles/get_element_client.dir/src/get_element_client.cpp.o] Error 1
make[1]: *** [beginner_tutorials/CMakeFiles/get_element_client.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

Could you help me to learn how to declare the array to make it work? Thanks to all


Originally posted by javietos on ROS Answers with karma: 1 on 2016-07-11

Post score: 0


Original comments

Comment by ahendrix on 2016-07-11:
It looks like most of the error message is missing; please include the full error message from the compiler.

Comment by javietos on 2016-07-11:
Now I include all the error message, if you prefer I can add some images to see it better.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The first errors messages are often the most important. In your errors the important ones seem to be:

/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:7:7: error: ‘beginner_tutorials::GetElement::Response’ has no member named ‘result’

and

/home/javier/catkin_ws/src/beginner_tutorials/src/get_element_server.cpp:8:33: error: ‘beginner_tutorials::GetElement::Request’ has no member named ‘name’

these errors are repeated several more times in various different contexts within your code.

These errors seem odd, because as listed these fields are present in your service definition.

I would start by checking:

  • You list your service definition file as GetElement.h; this should be GetElement.srv
  • You should confirm that C++ headers are being generated from your service definition. They should be automatically generated in the devel/include/path/to/package/GetElement.h file in your catkin workspace.
  • Check that you're including the service definition from the correct package

Originally posted by ahendrix with karma: 47576 on 2016-07-11

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by javietos on 2016-07-11:
Sorry, my fault. I had the names in spanish and at the time of changing them to english I forgot those. Now the error is much shorter.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.