Resolve the problem that ROS_INFO cannot output string correctly
Today, I will talk to you about solving the problem that ROS_INFO cannot correctly output string. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
First, output "? "
The project debugs a node, and when printing ROS information, it is found that the name of the node set is a question mark:
ROS_INFO ("[% s]: camera_extrinsic_mat", kNodeName)
After looking at the code, I found that the node name was set to const, but the const variable was not initialized correctly, resulting in the output "? C++ syntax is too lame. Here is the first way to initialize const:
/ / 1. Define it in the class first
Private:
Const std::string kNodeName
/ / 2. Initialize after the constructor initializes the list
ClassName (): kNodeName ("node_name")
The second way is to add the static keyword:
/ / 1. Define static const variables in a class
Private:
Static const std::string kNodeName
/ / 2. Const initialization outside the class
Const std::string ClassName::kNodeName = "node_name"
The second way I use, because this const looks intuitive, it is not easy to see the assignment in the constructor initializer list. Here is my modified code:
/ / 1. Lidar_camera_fusion.h
Private:
Static const std::string kNodeName
/ / 2. Lidar_camera_fusion.cpp
Const std::string LidarCameraFusion::kNodeName = "lidar_camera_fusion"
/ / 3. ROS_INFO
ROS_INFO ("[% s]: camera_extrinsic_mat", kNodeName)
Summarize the use of const variables in the class:
Initialize the const variable in the constructor parameter initialization list, declare the const variable as static, and then initialize it outside the class. Second, output garbled.
After initializing the const variable correctly, it is found that INFO outputs garbled codes:
After looking for the following information, it is found that ROS_INFO cannot directly export std::string, so it needs to be converted to c_str:
/ / 3. ROS_INFO
ROS_INFO ("[% s]: camera_extrinsic_mat", kNodeName.c_str ())
The second reason for garbled code is that the% s is miswritten as the uppercase% S, and you can change it back:
/ / 3. Mistakenly written in capital% S.
ROS_INFO ("[% S]: camera_extrinsic_mat", kNodeName.c_str ())
After reading the above, do you have any further understanding of solving the problem that ROS_INFO cannot output string correctly? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.