代码之家  ›  专栏  ›  技术社区  ›  Vtik

在C++11中运行后的模板类错误

  •  -2
  • Vtik  · 技术社区  · 8 年前

    我正在使用PCL开发ROS包。

    我遇到的问题是,它可以用C++98和C++11成功编译。 但是,当该节点在C++11支持的版本(CMakeLists L:5,未注释)中启动时,它会立即出现故障,尽管在没有C++11的情况下编译时效果很好。

    相同的代码体系结构(模板库) this

    我发现模板图书馆是一个持有的原因。

    平台:Ubuntu 14.04,ROS Indigo,GCC 4.8.4。

    谢谢

    #include <ros/ros.h>
    #include "pcl_tools.h"
    // PCL specific includes
    #include <sensor_msgs/PointCloud2.h>
    #include <pcl_conversions/pcl_conversions.h>
    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include <pcl/filters/voxel_grid.h>
    using namespace pcl_tools;
    ros::Publisher pub;
    
    void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
    {
      /// ros to pcl cloud
      pcl::PCLPointCloud2 cloud2_in;
      pcl_conversions::toPCL(*input,cloud2_in);
      pcl::PointCloud< pcl::PointXYZ>::Ptr temp_cloud(new pcl::PointCloud< pcl::PointXYZ>);
      pcl::fromPCLPointCloud2(cloud2_in,*temp_cloud);
    
      /// use templated function
      pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>);
      cloud_filtered = passthroughfilter<pcl::PointXYZ>(temp_cloud, "y", -2, 2.0);
    }
    
    int main (int argc, char** argv)
    {
      ros::init (argc, argv, "template_test");
      ros::NodeHandle nh;
      ros::Subscriber sub = nh.subscribe ("/camera/depth/points", 1, cloud_cb);
      ros::spin ();
    }
    

    #ifndef PCL_TOOLS
    #define PCL_TOOLS
    
    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include <pcl/filters/passthrough.h>
    #include <string>
    #include <sstream>
    #include <assert.h>
    
    namespace pcl_tools
    {
        template <typename pointT> boost::shared_ptr< pcl::PointCloud<pointT> >
        passthroughfilter(boost::shared_ptr< pcl::PointCloud<pointT> >, std::string, float, float, bool invert = false);
    }
    
    template <typename pointT>
    boost::shared_ptr< pcl::PointCloud<pointT> > pcl_tools::passthroughfilter(boost::shared_ptr< pcl::PointCloud<pointT> > input,
                                                                    std::string axis, float min , float max, bool invert = false)
    {
        typedef boost::shared_ptr< pcl::PointCloud<pointT> > PointCloudPtr;
        PointCloudPtr cloud(new pcl::PointCloud<pointT>);
    
        // Create the filtering object
        pcl::PassThrough<pointT> pass;
        pass.setInputCloud (input);
        pass.setFilterFieldName (axis);
        pass.setFilterLimits (min, max);
        pass.setFilterLimitsNegative (invert);
        pass.filter (*cloud);
        return cloud;
    }
    #endif
    

    CMakeLists。txt文件

    cmake_minimum_required(VERSION 2.8.3)
    project(test_template)
    
    ## Compile as C++11, supported in ROS Kinetic and newer
    add_compile_options(-std=c++11)
    
    find_package(catkin REQUIRED COMPONENTS
      cv_bridge
      geometry_msgs
      image_transport
      pcl_conversions
      pcl_ros
      roscpp
      rospy
      sensor_msgs
      std_msgs
    )
    
    add_definitions(${PCL_DEFINITIONS})
    catkin_package()
    
    include_directories(
        include/${PROJECT_NAME}
        ${catkin_INCLUDE_DIRS}
        ${PCL_INCLUDE_DIRS}
    )
    
    link_directories(${PCL_LIBRARY_DIRS})
    
    add_executable(pcl_template_test src/pcl_template.cpp)
    target_link_libraries(pcl_template_test ${catkin_LIBRARIES} )
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Vtik    8 年前

    事实证明这是PCL库抛出的。事实上,通过apt get安装PCL会安装不兼容C++11的版本。因此,为了克服这个问题,我不得不从源代码处重新编译它,添加了C++11支持。关于这个问题的更多信息 here