在 C++11 中 运行 之后模板化 class 错误

templated class faults after running in C++11

我正在使用 PCL 开发 ROS 包。

我遇到的问题是它使用 C++98 和 C++11 编译成功。 但是,当节点在支持 C++11 的版本(CMakeLists L:5,未评论)中启动时,它会立即出现段错误,尽管在没有 C++11 的情况下编译时它工作正常。

this 包的相同代码架构(模板库)显示相同的行为。

我发现模板库就是原因所在。 但是,我还想不通。我在这里错过了什么?

平台:Ubuntu14.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 ();
}

模板header:

#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} )

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