请教c++中类模板的问题

来源:百度知道 编辑:UC知道 时间:2024/06/13 10:10:56
/*
以下代码没有任何实际意义,编译无法通过。
请教各位高手,这些代码,语法上有什么错误 ,
*/
#include <iostream>
using namespace std;
template <class T1,class T2>
class test
{
public:
test():a(0),c(0){}
test(T1 one,T1 two):a(one),c(two){}
test(T1 one,T2 two):a(one),b(two){}
T1 display1();
T2 display2();
private:
T1 a,c;
T2 b,d;
};
template <class T1,class T2>
T1 test<T1,T2>::display1()
{
return (a+c);
}
template <class T1,class T2>
T2 test<T1,T2>::display2()
{
return (b+d);
}
int main()
{
test<int,int> kkk(43,54);
test<double,double> ab(43.43,553.53);
test<double,int> cd(4324.43,434);
test<int,double> ef(34,42.46);

cout<<kkk.display()
<<'\n'<

test(T1 one,T1 two):a(one),c(two){}
test(T1 one,T2 two):a(one),b(two){}

test<int,int> kkk(43,54);
test<double,double> ab(43.43,553.53);

这2句对应上面的构造函数在类模板实例化的时候函数特征标完全相同,无法重载。

#include <iostream>
using namespace std;
template <class T1,class T2 = T1>
class test
{
public:
test(): a(0), b(0){}
test(T1 one, T2 two):a(one),b(two){}
T1 display1();
T2 display2();
private:
T1 a;
T2 b;
};

template <class T1,class T2>
T1 test<T1,T2>::display1()
{
return static_cast<T1>(a + b);
}
template <class T1,class T2>
T2 test<T1,T2>::display2()
{
return static_cast<T2>(a + b);
}

int main()
{
test<int> kkk(43,54);
test<double> ab(43.43,553.53);
test<double, int> cd(4324.43,434);
test<int, double> ef(34,42.46);

cou