谁能告诉我在使用 PCL 时使用 ::Ptr 和 new 运算符的事情

Could anyone tell something about using ::Ptr and new operator when using PCL

我试图用

声明一个 PCL class 的对象
typedef pcl::PointCloud<PointNT> PointCloudT;   // in the .h file.

PointCloudT::Ptr object = new PointCloudT();   // in main func
PointCloudT::Ptr scene = new PointCloudT();    // and these two triggers the error of

1>f:\cpps\pclrclass\pclrclass\main_routine.cpp(10): error C2440: 'initializing' : 无法从 'PointCloudT *' 转换为 'boost::shared_ptr>'

但如果声明由

给出则有效
PointCloudT::Ptr object(new PointCloudT);   // in main func
PointCloudT::Ptr scene(new PointCloudT);    // works and correct

我不是很会C++,也不知道这个错误是什么问题。谁能介绍一下。

new PointCloudT() 表达式生成指向点云的原始指针,PointCloudT *

PointCloudT::Ptrboost::shared_ptr<PointCloudT>的同义词,是指向点云的智能指针。您可以查看此模板的概要class here。如您所见,它有一个构造函数

  template<class Y> explicit shared_ptr(Y * p);

在我们的例子中变成了

  explicit shared_ptr(PointCloudT * p);

这意味着它可以从指向点云的原始指针构造。注意 explicit keyword,它禁止隐式构造和复制初始化。

这就是为什么第一个代码片段出错(您请求在两个不同的 classes 之间进行隐式转换),而第二个代码片段有效(您显式调用共享指针的构造函数)。