在类中声明tf2_ros::Buffer时生成错误

Building error when declare tf2_ros::Buffer in class

本文关键字:Buffer 错误 ros 声明 tf2      更新时间:2023-10-16

我正在尝试在一个简单的代码中使用tf2_ros::Buffer。当我把它放在主功能中时,一切都很好。但当放入类中时,会发生构建错误。代码如下:

#include <ros/ros.h>
#include <tf2_ros/buffer.h>
#include <tf2_ros/transform_listener.h>
#include <geometry_msgs/TransformStamped.h>
#include <geometry_msgs/Twist.h>
class test_class
{
private:
double start;
double duration;
ros::Time start_time;
ros::Time end_time;
std::string robot_name;
tf2_ros::Buffer tf_buffer; // problem line
tf2_ros::TransformListener* tfListener;
geometry_msgs::TransformStamped transformStamped;
public:
std::string space_name;
std::string node_name;
test_class()
{
space_name = ros::this_node::getNamespace();
node_name = ros::this_node::getName();
}
~test_class()
{}
bool initialize(const ros::NodeHandle& n)
{
ROS_INFO("Class auto_mav_flight initialized done!");
return true;
}
void timer_callback(const ros::TimerEvent& event)
{
ROS_INFO("Timer Callback triggered.");
return;
}
}; 
int main(int argc, char** argv)
{
ros::init(argc, argv, "auto_mav_node");
ros::NodeHandle node;
ROS_WARN("The node is initilized and started.");
test_class amf = test_class();
amf.initialize(node);
ros::Timer timer_1 = node.createTimer(ros::Duration(0.5), &test_class::timer_callback, &amf);
ros::spin();
return EXIT_SUCCESS;
}

构建错误信息为:

/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp: In function ‘int main(int, char**)’:
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:73:44: error: no matching function for call to ‘test_class::test_class(test_class)’
test_class amf = test_class();
^
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:73:44: note: candidates are:
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:26:2: note: test_class::test_class()
test_class()
^
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:26:2: note:   candidate expects 0 arguments, 1 provided
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:9:7: note: test_class::test_class(test_class&)
class test_class
^
/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:9:7: note:   no known conversion for argument 1 from ‘test_class’ to ‘test_class&’
make[2]: *** [auto_mav_flight/CMakeFiles/auto_mav_flight_node.dir/src/node_main.cpp.o] Error 1
make[1]: *** [auto_mav_flight/CMakeFiles/auto_mav_flight_node.dir/all] Error 2
make: *** [all] Error 2

我发现,如果我对声明tf2_ros::buffer:的代码行进行注释

tf2_ros::Buffer tf_buffer;

错误消失。

为什么tf2_ros::Buffer会导致类构造问题,即使我只是将其声明为类的成员?

任何帮助都将不胜感激。

提前谢谢。

从此:

/home/arkin/ros_code/sharbox/auto_mav_sharbox/src/auto_mav_flight/src/node_main.cpp:26:2:注意:候选者需要0个参数,而提供了1个/home/arkin/ros_code/sharbox/auto_mav_sharbox/src/auto_mav_flight/src/nod_main.cpp:9:7:注意:test_class::test_cclass(test_class&)

您似乎正在调用test_class的复制构造函数(可能隐藏在ROS层中,通过尝试将test_class作为函数参数传递或在使用容器时传递)。

从tf2_ros::Buffer头中,它继承自BufferCore,后者包含一个boost::mutex(除其他外,可能有多个不可复制的属性),该属性不可复制构造。这使得tf2_ros::Buffer不可复制构造。由于test_class不定义复制构造函数并且包含不可复制的属性,因此编译器无法生成复制构造函数,并且在尝试调用复制构造函数时无法编译。

供参考:http://en.cppreference.com/w/cpp/language/copy_constructor