利用ROS编写一个输出“hello world”简单实例出现“terminate called after throwing an instance of 'ros::TimeNotInitializedException'
what(): Cannot use ros::Time::now() before the first NodeHandle has been created or ros::start() has been called. If this is a standalone app or test that just uses ros::Time and does not communicate over ROS, you may also call ros::Time::init()”的解决方法!!!
代码如下:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>using namespace ros;using namespace std;int main(int argc,char ** argv)
{ros::init(argc,argv,"message_output");ros::Rate loop_rate(1);int count = 0; while(ros::ok()){ROS_INFO("Hello world");count++;loop_rate.sleep(); } return 0;
}
出现了以上的错误,原因是使用ros::Rate loop_rate(1)对象未初始化,需在ros::Rate loop_rate(1)前面添加ros::Time::init();语句进行初始化