模板使用出错。帮我解决它。谢谢!

来源:百度知道 编辑:UC知道 时间:2024/08/23 09:03:57
代码:
#include<iostream>
using namespace std;
template <class T>
class pair{
T value1,value2;
public:
pair(T first,T second)
{
value1=first;
value2=second;
}
T module(){return 0;}
};
template <>
class pair<int>
{
int value1,value2;
public:
pair(int first,int second)
{
value1=first;value2=second;
}
int module();
};
template<>
int pair::module()
{
return value1%value2;
}
int main(){
pair myints(100,75);
pair myfloats(100.0,75.0);
cout<<myints.module()<<"\n"
<<myfloats.module()<<endl;
system("pause");
}

错误提示:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\cpp\1005-1

/* MinGW 下通过, Dev-cpp也是用MinGW的吧, 其实MinGW的g++和规范有些不一样,比如规范里class和typename是一样的,MinGW下不一样, 把你原来编译的obj和exe文件都删干净,你的IP是局域网的吧。刚下dev-cpp4.99试过,编译通过,运算结果 25 0 */

#include<iostream>

using std::cout;
using std::endl;

template<class T> class pair {
T value1, value2;
public:
pair(T first, T second) {
value1=first;
value2=second;
}
T module() {
return 0;
}
};

template<> class pair<int> {
int value1, value2;
public:
pair(int first, int second) {
value1=first;
value2=second;
}
int module();
};

int pair<int>::module() {
return value1%value2;
}

int main(void) {
pair<int> myints(100, 75);
pair<float> myfloats(100.0, 75.0);
cout<<myints.module()<<"\n"<<myfloats.module()<<endl;

return 0;
}

错误一堆堆的