Return array in service in c++
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 ...
It looks like most of the error message is missing; please include the full error message from the compiler.
Now I include all the error message, if you prefer I can add some images to see it better.