使用模板创建类时出现的问题

问题描述

我做错了什么?

#include "String.hpp"

namespace java {
      namespace lang {


template<typename  T>
class Comparable<T>  {

protected:
     virtual int8_t compareto(T& o);
};

}


}



#endif // COMParaBLE_H

我之前没有在任何地方声明过这个类

Comparable.h:11:7: error: explicit specialization of undeclared template class 'Comparable'

解决方法

定义模板类时,类名中不需要模板参数:

template<typename  T>
class Comparable<T>  { /* ... */ };
//              ^^^
//     Invalid here

只需删除那部分:

template<typename  T>
class Comparable  { /* ... */ };