Look at the definition of addPointCloud()
more carefully:
bool pcl::visualization::PCLVisualizer::addPointCloud(
const pcl::PointCloud<pcl::PointXYZ >::ConstPtr & cloud,
const std::string & id = "cloud",
int viewport = 0
)
Its first parameter is a const
reference to a pcl::PointCloud<pcl::PointXYZ>::ConstPtr
, where ConstPtr
is a typedef for boost::shared_ptr<const PointCloud<pcl::PointXYZ>>
.
You are trying to pass a pcl::PointCloud<pcl::PointXYZ>*
where a boost::shared_ptr<const PointCloud<pcl::PointXYZ>>
is expected. That does not match the definition.
boost::shared_ptr
has an explicit
constructor for single-value input, so the compiler cannot implicitly create a temporary boost::shared_ptr<const PointCloud<pcl::PointXYZ>>
for your pcl::PointCloud<pcl::PointXYZ>*
value.
To get around that, you will have to invoke the explicit constructor directly:
pcl::PointCloud<pcl::PointXYZ> linaccpc;
const pcl::PointCloud<pcl::PointXYZ> *linptr = &linaccpc;
...
linplotter->addPointCloud<pcl::PointXYZ> (pcl::PointCloud<pcl::PointXYZ>::ConstPtr(linptr), "linear acc", 0);
Or:
pcl::PointCloud<pcl::PointXYZ> linaccpc;
pcl::PointCloud<pcl::PointXYZ>::ConstPtr linptr (&linaccpc);
...
linplotter->addPointCloud<pcl::PointXYZ> (linptr, "linear acc", 0);
Note, however, that this will likely crash your code. By default, shared_ptr
calls delete
on any pointer you give it, so it will be calling delete
on a memory address it has no business freeing. So allocate the pointer using new
instead:
pcl::PointCloud<pcl::PointXYZ>::ConstPtr linptr (new pcl::PointCloud<pcl::PointXYZ>);
...
linplotter->addPointCloud<pcl::PointXYZ> (linptr, "linear acc", 0);
If you want to get around that and a variable whose lifetime you control, but that shared_ptr
simply holds a pointer to, you will have to provide a custom deleter
that will not free the variable memory when the shared_ptr
is done using the pointer:
template <class T>
static void DoNotFree(T*)
{
}
...
pcl::PointCloud<pcl::PointXYZ> linaccpc;
pcl::PointCloud<pcl::PointXYZ>::ConstPtr linptr (&linaccpc, &DoNotFree<pcl::PointCloud<pcl::PointXYZ>>);
...
linplotter->addPointCloud<pcl::PointXYZ> (linptr, "linear acc", 0);
This last approach will only work if linplotter
does not need to keep using the linaccpc
variable after it goes out of scope, such as if linplotter
is destroyed before lineaccpc
. This is not something you should rely on, though. The primary purpose of shared_ptr
is to manage an object's lifetime for you, so let it do its job. Use new
.