只是为了扩展@Sterling已经解释过的内容......
如果您有一个名为“test_messages”的项目(以及目录),并且您有以下两种类型的消息test_messages/msg
:
#> cat test.msg
string first_name
string last_name
uint8 age
uint32 score
#> cat test_vector.msg
string vector_name
uint32 vector_len # not really necessary, just for testing
test[] vector_test
然后,您可以编写以下 C++ 代码:
#include "test_messages/test.h"
#include "test_messages/test_vector.h"
...
::test_messages::test test_msg;
test_msg.age = 29;
test_msg.first_name = "Firstname";
test_msg.last_name = "Lastname";
test_msg.score = 79;
test_pub.publish(test_msg);
::test_messages::test_vector test_vec;
test_vec.vector_len = 5;
test_vec.vector_name = std::string("test vector name");
for (int idx = 0; idx < test_vec.vector_len; idx++)
{
test_msg.age = 50;
test_msg.score = 100;
test_msg.first_name = std::string("aaaa");
test_msg.last_name = std::string("bbbb");
test_vec.vector_test.push_back(test_msg);
}
test_vector_pub.publish(test_vec);