문제

I would like to know if this is possible. I have a function:

 pcl::PointCloud<pcl::PointXYZRGB> createPointCloud(std::Vector<Nodes> input)

which returns a point cloud. I would like to know if it is possible to take this point cloud, and make a pointer to a copy of it. pcl makes pointers to clouds like this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>)

I have tried doing this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(createPointCloud(nodeList))

This results in a pretty obvious error ie. createPointCloud doesnt return a pointer to a cloud.

I have also tried this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR = &createPointCloud(nodeList)

and this:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(&createPointCloud(nodeList))

And this results in the compile error: "taking address of temporary"

Is the only option to have the function return a pointer type or is there a way to do what i am asking?

EDIT:

Both of the below answers are correct, I have awarded Jonathon the correct tick as he got in first this time.

도움이 되었습니까?

해결책

I know this is old and probably of no more use to OP, but other users might stumble upon it. I would suggest doing it as follows:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZRGB>);
*cloudPTR = createPointCloud(nodeList);

The reason Jonathon's answer is dangerous is that Pointcloud::Ptr is a typedef for a boost::shared_ptr which implies ownership of the object pointed to. In his answer, however, the object is actually a local variable meaning that it might go out of scope while there are still references to it and that shared_ptr will eventually call delete on it, which is undefined behavior.

Using make_shared() on the other hand deep copies the cloud. The program will work correctly, but if you didn't need the extra copy, it is far from optimal.

다른 팁

Yes, use the makeShared() method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top