2017-10-10 672 views
2

我想在簡單的代碼中使用tf2_ros :: Buffer。當我把它放在主要功能中時,一切正常。但是,當放入課堂時,會出現建築物錯誤。該代碼是這樣的:聲明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 ::緩衝區:

tf2_ros::Buffer tf_buffer; 

錯誤消失。

爲什麼tf2_ros :: Buffer會導致類構造問題,即使我只是將它聲明爲類的成員?

任何幫助將不勝感激。

在此先感謝。

回答

1

從這:

/home/arkin/ros_code/sandbox/auto_mav_sandbox/src/auto_mav_flight/src/node_main.cpp:26:2: 注:候選人預計0參數,1提供 /家用/阿爾金/ ros_code /沙/ auto_mav_sandbox/src目錄/ auto_mav_flight/src目錄/ node_main.cpp:9:7: 注:爲test_class ::爲test_class(爲test_class &)

看來您呼叫的拷貝構造函數test_class(可能隱藏在圖層中ROS,試圖通過test_class作爲函數參數或使用容器時)。

tf2_ros::Buffer標題開始,它繼承自BufferCore,其中包含boost::mutex(其中包含超過1個不可複製的屬性),它不是可複製構建的。這使得tf2_ros::Buffer不可複製。 由於test_class未定義複製構造函數並且包含非可複製屬性,因此編譯器無法生成複製構造函數,並且在嘗試調用複製構造函數時無法編譯。

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

+0

感謝您的答覆。是的,這個類需要一個拷貝構造函數來完成類的初始化:'test_class amf = test_class();'如果我用'test_class amf;'代替這個代碼行,它就行。當然,有必要刪除用戶定義的類構造函數的操作。另一種方法是定義一個拷貝構造函數(甚至是一個空構造函數),如:'test_class(const test_class&right_side)'。這也解決了這個問題。看來實現'tf2_ros :: Buffer'不是copy-constructibal是解決這個問題的關鍵。再次感謝。 – Arkin

+0

請考慮接受答案:) – Clonk