显式模板专用化错误
Explicit template specialization error
这个应该很容易。我正在使用模板,但收到编译器错误。
#include <iostream>
template <class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1& first();
T2& second();
Pair(const T1& aval, const T2& bval) : a(aval), b(bval) {}
};
template <class T1, class T2>
T1& Pair<T1,T2>::first()
{
return a;
}
template <class T1, class T2>
T2& Pair<T1,T2>::second()
{
return b;
}
// Explicit Specialization
template <>
class Pair<double, int>
{
private:
double a;
int b;
public:
double& first();
int& second();
Pair(const double& aval, const int& bval) : a(aval), b(bval) {}
};
template <>
double& Pair<double,int>::first()
{
return a;
}
template <>
int& Pair<double,int>::second()
{
return b;
}
int main(int argc, char const *argv[])
{
Pair<int, int> pair(5,6);
//Pair<double,int> pairSpec(43.2, 5);
return 0;
}
错误如下所示
main.cpp:42:27: error: no function template matches function template specialization 'first'
double& Pair<double,int>::first()
^
main.cpp:49:24: error: no function template matches function template specialization 'second'
int& Pair<double,int>::second()
有什么线索可以发现可能出现什么问题吗?
在方法声明之前不需要模板<>声明。
double& Pair<double,int>::first() {
return a;
}
int& Pair<double,int>::second() {
return b;
}
应该足够了。
由于另一个答案没有解释为什么这里不需要前缀 template<>
,我将尝试在我的答案中提供该解释。
问题的解决方案
如下所述,我们只需要删除template<>
前缀,如下所示:
//no prefix template<> needed here
inline double& Pair<double,int>::first()
{
return a;
}
//no prefix template<> needed here
inline int& Pair<double,int>::second()
{
return b;
}
工作演示
请注意,添加了 inline
关键字,这样我们就不会收到多个定义错误,因为通常我们在头文件中实现模板,然后包含在多个源文件中。
问题解释
我们不需要前缀template<>
的原因是,我们为完整类模板专用化的成员函数提供了一个普通的类外定义。也就是说,我们实际上并没有专门研究成员函数,而是为这些成员函数提供了一个普通的(非模板)类外定义。
相关文章:
- C++类错误中的模板专用化
- 模板专用化会导致未定义的引用错误
- flat_hash_map.h:错误 C3203:"templated_iterator":非专用类模板
- C++ 将派生类的成员函数指针作为参数传递时选择了错误的模板专用化
- cpp 模板专用化,错误说参数 1 的类型为 T,这取决于参数 T
- 类模板部分专用化:编译器错误
- 为什么在源文件中专用模板不会导致错误?
- 函数模板(它是类模板的成员)的显式专用化会产生"partial specialization is not allowed"错误,为什么?
- C++模板 - 使用"std::is_same_v"而不是专用并避免编译错误?
- VS2017模板专用化错误无法从'Class *(__cdecl *)(Args...)'转换为'Class *(__cdecl *)(Args...)'
- 函数模板专用化生成链接错误
- 错误 C2908:显式专用化; 已被实例化
- 部分模板专用化错误
- 模板函数专用化的内部编译器错误
- 类模板方法的专用化,类型名称是类模板 - 错误:参数处的类型/值不匹配
- 错误 C2893:无法使用 CTPL 专用化函数模板
- gcc中变量模板的错误显式模板专用化
- Qt nmake:错误 C3203:"map":非专用类模板不能用作模板参数"base"的模板参数,预期为真实类型
- 声明无法解决"实例化后的显式专用化"错误
- 需要"override"案例时重叠模板部分专用化:如何避免错误?