error: ‘rosout’ in namespace ‘ros’ does not name a type
时间: 2023-09-25 09:13:23 浏览: 172
抱歉,我之前的程序存在一些问题,导致编译时出现了错误。在ROS的最新版本中,`rosout`已经被移除了,因此您需要使用`rosgraph_msgs`包中的`Log`消息来代替。以下是更新后的程序:
```cpp
#include <ros/ros.h>
#include <ros/console.h>
#include <rosgraph_msgs/Log.h>
#include <iostream>
int main(int argc, char** argv)
{
ros::init(argc, argv, "log_listener");
ros::NodeHandle nh;
// 设置日志级别为DEBUG
if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug))
{
ros::console::notifyLoggerLevelsChanged();
}
// 订阅/rosout话题
ros::Subscriber sub = nh.subscribe("/rosout", 1000, [](const rosgraph_msgs::Log& msg) {
// 解析日志级别
std::string level_string;
switch (msg.level) {
case ros::console::levels::Debug:
level_string = "\033[34m[DEBUG]\033[0m";
break;
case ros::console::levels::Info:
level_string = "\033[32m[INFO]\033[0m";
break;
case ros::console::levels::Warn:
level_string = "\033[33m[WARN]\033[0m";
break;
case ros::console::levels::Error:
level_string = "\033[31m[ERROR]\033[0m";
break;
case ros::console::levels::Fatal:
level_string = "\033[35m[FATAL]\033[0m";
break;
default:
level_string = "\033[0m";
}
// 解析文件名和代码行数
std::string location_string = msg.file + "-" + std::to_string(msg.line);
// 解析时间
std::string time_string = ros::Time(msg.header.stamp).toBoost().time_since_epoch().count();
// 输出日志
std::cout << level_string << "[" << location_string << "][" << time_string << "]: " << msg.msg << std::endl;
});
ros::spin();
return 0;
}
```
更新后的程序使用`rosgraph_msgs`包中的`Log`消息来代替旧版本中的`rosout`消息,同时也更新了Lambda表达式中的函数参数类型。希望这个程序能够解决您遇到的问题!
阅读全文