ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
The problem is that the msg.name
field is defined in the message definition as string[] name
. According to this documentation, messages with variable length arrays get treated as C++ std::vector
datatypes. So when you instantiate a new message in your second line, the size of the name field's std::vector
is not set. Thus, using msg.name[0]
throws a seg fault because you are trying to access unallocated memory. So you either need to declare the size of the vector before trying to access the elements, or use dynamic allocating methods like std::vector::push_back
. So you could for example change the last line to
msg.name.push_back("joint1");
See this page for more information about the vector class.