X不是类模板

Gha*_*ham 1 c++ templates

我正在尝试编译以下代码,但出现错误

“错误:“ MD5Sum”不是类模板
template <>结构MD5Sum <:: cv_bridge :: CvImage>”

template<> struct MD5Sum<::cv_bridge::CvImage>
{
  static const char* value() { return MD5Sum<::sensor_msgs::msg::Image>::value(); }
  static const char* value(const ::cv_bridge::CvImage&) { return value(); }

  static const uint64_t static_value1 = MD5Sum<::sensor_msgs::msg::Image>::static_value1;
  static const uint64_t static_value2 = MD5Sum<::sensor_msgs::msg::Image>::static_value2;

  // If the definition of sensor_msgs/Image changes, we'll get a compile error here.
  ROS_STATIC_ASSERT(MD5Sum<::sensor_msgs::msg::Image>::static_value1 == 0x060021388200f6f0ULL);
  ROS_STATIC_ASSERT(MD5Sum<::sensor_msgs::msg::Image>::static_value2 == 0xf447d0fcd9c64743ULL);
};
Run Code Online (Sandbox Code Playgroud)

这似乎是模板专业化。这是否意味着在使模板专门化之前,应该首先存在模板类。

amc*_*176 5

类模板MD5Sum需要在其任何专业化之前声明。您要么需要包含声明模板的文件,要么自己声明它。

您可以使用空定义(如果您只想调用完全专门的版本)或选择通用实现:

// add this before your specialization
template <class T> struct MD5Sum; // empty declaration

// or your default implementation
template <class T> struct MD5Sum {
   ...
};
Run Code Online (Sandbox Code Playgroud)