防止模板部分专门化错误
Prevent template partial specialization error
有一个代码:
#include <functional>
template<typename DataType, typename Compare=std::less<DataType>>
class MyClass {
public:
explicit MyClass(const Compare& f = Compare()) {
compare = f;
};
bool foo(DataType, DataType);
private:
Compare compare;
};
template<typename DataType>
bool MyClass<DataType>::foo(DataType a, DataType b) {
return compare(a, b);
}
在编译时得到一个错误:
error: nested name specifier 'MyClass<DataType>::'
for declaration does not refer into a class, class template or class
template partial specialization bool MyClass<DataType>::foo(DataType a, DataType b) {
如何防止错误并在类外声明方法?
您必须像在主模板定义中那样提供模板参数:
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvv
template <typename DataType, typename X>
bool MyClass<DataType, X>::foo(DataType a, DataType b) {
// ^^^^^^^^^^^
return compare(a, b);
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- 线程 std::调用未知类型,无法专门化函数错误
- 错误 C2893 无法专门化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...)'
- 如果模板的错误代码在If(false)中,我必须专门化模板吗
- 错误:T没有命名类型-用于使用强类型枚举的专门化
- 显式函数模板专门化选择了错误的专门化
- 下面的模板专门化代码是非标准的,或者是vs - c++中的错误
- 为类的trait专门化实现错误消息
- 当试图用另一个类模板部分专门化一个类模板时,在VS2013中出现错误
- 部分和显式模板专门化的奇怪错误
- 防止模板部分专门化错误
- 模板专门化GCC的语法错误,但MSVC没有
- 在cpp文件中实现模板专门化导致Template -id does not match错误
- 模板专门化语法错误?不确定
- 私有类专门化错误
- 一个模板专门化用于pod,一个模板专门化用于类层次结构,在其他情况下出现错误
- GCC错误:在非命名空间范围内显式专门化
- 模板方法专门化链接错误