我现在正在使用 PCL(点云库)处理 C++ 中的模板,我遇到了一些我无法解决的问题(我之前在互联网和 Stack 上搜索过)
我有一个名为 Features
的模板类。
我的 hpp 文件:
#ifndef KeyFeatures_hpp
#define KeyFeatures_hpp
// Declarations and includes
typedef pcl::PointXYZRGB PointTypeRGB;
template<typename FeatureType>
class Features {
public:
Features(const int typeDescriptor);
void setDescriptorExtractor(typename pcl::Feature<PointTypeRGB, FeatureType>::Ptr extractor);
private:
typename pcl::Feature<PointTypeRGB, FeatureType>::Ptr m_descriptor_extractor;
};
#endif /* Features_hpp */
在 cpp 文件中,我有一个构造函数,它会检查它是什么类型,然后执行一些操作。
template <typename FeatureType>
Features<FeatureType>::Features(const int type){
//Some code
if (type == DESCRIPTOR_SHOT){
pcl::SHOTEstimationOMP<PointTypeRGB, pcl::Normal, pcl::SHOT352>* shot = new pcl::SHOTEstimationOMP<PointTypeRGB, pcl::Normal, pcl::SHOT352>;
shot->setRadiusSearch (0.02f);
pcl::Feature<PointTypeRGB, pcl::SHOT352>::Ptr descriptor_extractor (shot);
descriptor_extractor->setSearchMethod (pcl::search::Search<PointTypeRGB>::Ptr (new pcl::search::KdTree<PointTypeRGB>));
this->m_descriptor_extractor = descriptor_extractor;//ERROR
setDescriptorExtractor(descriptor_extractor);//ERROR
// Some code
}
当我尝试填写我的变量成员但没有成功时,错误出现在最后两行。 每次出现如下错误x 10(对应我的10种类型)
error: no matching conversion for functional-style cast from 'const shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352> >'
to 'this_type' (aka 'shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980> >')
然而,在我的 cpp 文件的末尾,我放置了所有的模板类。例如:
template class Features<pcl::SHOT352>;
在我的主函数中,我通过使用调用了这个类:
Features<pcl::SHOT352> feature_SHOT(type);
它似乎无法执行转换..
有人可以帮助我吗?
感谢
最佳答案
显然你正在实例化 Features<pcl::ShapeContext1980>
所以它的 m_descriptor_extractor
类型为 pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980>::Ptr
这是shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980>>
.
但在构造函数中您仍在使用 pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352>::Ptr
, 即 shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352>>
- 完全不同的类型。
作为旁注,you generally don't implement templates in .cpp files .
关于C++ 模板 - 没有可行的转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174126/