1

I'm now dealing with template in c++ with PCL (Point Cloud Library) and I encountered something that I'm not able to solve (I searched before on the internet and on Stack)

I have a template class called Features.

my hpp file:

#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 */

In the cpp file I have a constructor which will checks what kind of type is it and then perform some action.

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
}

The error appeared in the last two lines when I try without success to fill in my variable member. Each time I have the following error x 10 (corresponding to my 10 kind of type)

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> >')

However at the end of my cpp file I put all my template class. For example:

template class Features<pcl::SHOT352>;

In my main function I called this class by using:

Features<pcl::SHOT352> feature_SHOT(type);

It seems that it's not able to perform the conversion..

Can someone might be able to help me ?

Thank

lilouch
  • 1,054
  • 4
  • 23
  • 43

2 Answers2

1

Apparently you are instantiating Features<pcl::ShapeContext1980> so its m_descriptor_extractor has the type pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980>::Ptr which is shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980>>.

But inside the constructor you are still using pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352>::Ptr, that is shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352>> - a totally different type.

And as a side note, you generally don't implement templates in .cpp files.

Community
  • 1
  • 1
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
  • I edited my code. I constructed my Feature object with a specific type (here SHOT one) that's why I don't understand why I have this error 10 times corresponding to my 10 others descriptors. Concerning the template, I did that because otherwise my hpp file will be huge...And I wanted to keep clean. – lilouch Mar 23 '16 at 09:36
  • Because you do the same thing (with the wrong specific type) in all the 10 other descriptors. – Martin Bonner supports Monica Mar 23 '16 at 09:38
  • But when I constructed my object I called it in this way KeyFeatures feature_SHOT(type) ; so here the type is well specified and therefore in my constructor it should created the appropriate type no ? – lilouch Mar 23 '16 at 09:40
  • Did you read the link as to why templates need to go in headers? If you want to keep things clean, put the template methods in a .inl file ("inline"), and then include the .inl file in the header. (If you explicitly instantiate the templates, then that prevents others instantiating the templates with their types. That's a loss of flexibility, but if you don't need the flexibility, then don't worry about it.) – Martin Bonner supports Monica Mar 23 '16 at 09:40
  • @lilouch: You are explicitly instantiating the templates, so *all* methods are generated, and while your explicit type is fine for SHOT352, it doesn't work for ShapeContext1980 – Martin Bonner supports Monica Mar 23 '16 at 09:42
0

Firstly, the official term is a "class template" - a template for creating classes.

You appear to have created a class called Features<pcl::ShapeContext1980>.

This has m_descriptor_extractor of type shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::ShapeContext1980> >

You are trying to assign a shared_ptr<pcl::Feature<pcl::PointXYZRGB, pcl::SHOT352>> to it.

They are completely unrelated types, so it fails at compile time.

  • No that's the point, I didn't instantiate Features I just call template class Features at the end of my CPP file. – lilouch Mar 23 '16 at 09:42
  • OK. We need to see a [mcve]. Either your compiler has a serious bug, or you did instantiate Features . I am betting on the latter. Do you have a line `template class Features;`? That instantiates it. – Martin Bonner supports Monica Mar 23 '16 at 09:44
  • Yes at the end of my Features cpp file I called template class Features and for every type (About 10) in total ! – lilouch Mar 23 '16 at 09:47
  • Right. So that specific line *does* instantiate the template, and it instantiates *all* of the methods in the template. – Martin Bonner supports Monica Mar 23 '16 at 09:47
  • Okey I understand but if I don't put these lines, I cannot instantiate my other types. For example for another type if I call Features feature_FPFH(type); My compiler will say Undefined symbols for architecture x86_64: "Features::Features(int)" So how I can manage that ? – lilouch Mar 23 '16 at 09:51
  • 1
    Can you ever pass DESCRIPTOR_SHAPE_CONTEXT to the constructor of a `Features`? If not, I *suspect* that you need to create a `DescriptorExtractor` template function (with no body), and then explicitly specialize it for each of the feature types. The constructor of `Features` would look like `Features() : m_descriptor_extractor(DescriptorExtractor()) {}` – Martin Bonner supports Monica Mar 23 '16 at 10:12
  • I did what you mentioned that is to say make specialization of my constructor and ... it works ! Thank you ! I learned something :). – lilouch Mar 23 '16 at 14:17