将类实例移动到类成员后,无法初始化它

Cannot initialize a class instance, after I move it to a class member?

本文关键字:初始化 成员 实例 移动      更新时间:2023-10-16

这很有效:

pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2 (new pcl::PointCloud<pcl::PointXYZ>);

但这不起作用:

Class.h,私有变量

  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;

Class.cpp,在构造函数中

cloud (new pcl::PointCloud<pcl::PointXYZ>);

Make失败,原因为:

 error: no match for call to ‘(pcl::PointCloud<pcl::PointXYZ>::Ptr {aka boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> >}) (pcl::PointCloud<pcl::PointXYZ>*)’
     cloud (new pcl::PointCloud<pcl::PointXYZ>);

为什么两者不一样?从.cpp中看到的唯一区别是,一个是左边有类型(声明),另一个已经在.h中声明了,但尽管我在这两种方式中使用了完全相同的参数,但错误似乎都是关于参数的。

我认为您是在构造函数主体中初始化它,而不是在成员初始化器列表中:

struct A{
    A(int){} 
    A(){}
};
struct B
{
    A a;
    B(): a(52) //correct syntax
    {
        a(52); //error: no match for call to...
    }
};

int main()
{    
    A a(5); //ok this works
}

你必须放置cloud (new pcl::PointCloud<pcl::PointXYZ>);

在您的Class成员初始值设定项列表中:

Class(): cloud (new pcl::PointCloud<pcl::PointXYZ>)
{
//constructor body
}