[英]C++ Template - No viable conversion error
我現在正在用 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>>
- 完全不同的類型。
作為旁注,您通常不會在 .cpp 文件中實現模板。
首先,官方術語是“類模板”——用於創建類的模板。
您似乎創建了一個名為Features<pcl::ShapeContext1980>
。
這有m_descriptor_extractor
類型shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980> >
您正在嘗試為其分配shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352>>
。
它們是完全不相關的類型,因此在編譯時失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.