首页 > 解决方案 > 需要 'template<>' 语法 --> 通过函数调用类模板

问题描述

我有一个看起来像这样的标题类:

#ifndef A_H__
#define A_H__

using namespace pcl::tracking;

namespace ball_tracking_cloud
{

template <typename PointType>
class OpenNISegmentTracking
{
public:
  //...

protected:
   void update(const sensor_msgs::PointCloud2ConstPtr &input_cloud);

  }; // end of class

} // end namespace

#endif

现在我有一个如下所示的 .cpp 文件:

#include <ball_tracking_cloud/particle_detector.h>

bool init = true;


namespace ball_tracking_cloud
{
void OpenNISegmentTracking<pcl::PointXYZRGBA>::update(const sensor_msgs::PointCloud2ConstPtr &input_cloud)
{
    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBA>);
    pcl::fromROSMsg(*input_cloud, *cloud);

    if(init)
    {
        v.run ();
        init=false;
    }

   v.cloud_cb(cloud);
}

} // end of namespace

如果我编译我的代码,我会收到此错误:

: error: specializing member ‘ball_tracking_cloud::OpenNISegmentTracking<pcl::PointXYZRGBA>::update’ requires ‘template<>’ syntax
 void OpenNISegmentTracking<pcl::PointXYZRGBA>::update(const sensor_msgs::PointCloud2ConstPtr &input_cloud)
      ^
/hri/localdisk/markus/ros-alex/src/ball_tracking/ball_tracking_cloud/src/particle_detector.cpp:38:1: error: expected ‘}’ at end of input
 } // end of namespace
 ^

我不知道为什么会出现这个错误.....我想这与我使用模板类的事实有关......但我不确定这个......

任何帮助都会很棒!

标签: c++

解决方案


OpenNISegmentTracking就是c++所谓的完整模板专业化

换句话说,它是模板的一个版本,只有当模板参数是 a 时才会被调用pcl::PointXYZRGBA

这种定义的正确语法是

template <>
void OpenNISegmentTracking<pcl::PointXYZRGBA>::update(const sensor_msgs::PointCloud2ConstPtr &input_cloud)
{
    ...
}

推荐阅读