lundi 14 juin 2021

How to use Templated methods in C++?

In my header file, I have two different kind of objects ImageFrame and `PointCloudFrame. These are then defined with a std::variant as such

struct ImageFrame{

};

struct PointCloudFrame{

};

using SensorFrame = std::variant<std::shared_ptr<ImageFrame>,
                                std::shared_ptr<PointCloudFrame>>;

I need to write a template method for both these objects so I did it like this in the headerfile:

template<class T>
std::string getFrameBin(const T& frame);

And in the cpp file

template <class ImageFrame>
std::string getFrameBin(const ImageFrame& image_frame)
{
  std::string x;
  return x;
}

template <class PointCloudFrame>
std::string getFrameBin(const PointCloudFrame& cloud_frame)
{
  std::string x;
  return x;
}

However, I get the error relating to: error: redefinition of 'template<class PointCloudFrame> std::string

What am I doing wrong? Is there a more elegant way to do this to make use of the SensorFrame object

Aucun commentaire:

Enregistrer un commentaire