模板特殊化的格式的有关问题
模板特殊化的格式的问题
报错!
错误:‘int Pair<int>::show()’的模板标识符‘show<>’不匹配任何模板声明
第 43 行
正确格式怎么写,求大神帮忙!!!
------解决方案--------------------
template <>
class Pair <int>
已经是全特化了,所以后面的:
template <>
int Pair<int>::show()
不需要template<>了,直接
int Pair<int>::show()就行了。
#include <iostream>
using namespace std;
// Template specialization
template <class T> class Pair {
T value1, value2;
public:
Pair (T first, T second){
value1=first;
value2=second;
}
T show () {return 0;}
};
template <>
class Pair <int> {
int value1, value2;
public:
Pair (int first, int second){
value1=first;
value2=second;
}
int show ();
};
template <>
int Pair<int>::show()
{
return value1%value2;
}
int main () {
Pair <int> myints (100,75);
Pair <float> myfloats (100.0,75.0);
cout << myints.show() << '\n';
cout << myfloats.show() << '\n';
return 0;
}
报错!
错误:‘int Pair<int>::show()’的模板标识符‘show<>’不匹配任何模板声明
第 43 行
正确格式怎么写,求大神帮忙!!!
c++
模板
模板特殊化
------解决方案--------------------
template <>
class Pair <int>
已经是全特化了,所以后面的:
template <>
int Pair<int>::show()
不需要template<>了,直接
int Pair<int>::show()就行了。