本人试过在类模板外定义构造函数,编译不能通过,而在模板类内定义构造函数则能编译成功。
如在模板类外定义不成功:
#include"iostream"
using namespace std;
template
class Compare
{
public:
Compare(numtype a, numtype b);
/*{
x = a; y = b;
}*/
numtype max();
numtype min();
private:
numtype x, y;
};
template
numtype Compare::Compare(numtype a, numpyte b)
{
x = a;
y = b;
}
template
numtype Compare::max()
{
return((x > y) ? x : y);
}
template
numtype Compare::min()
{
return((x < y) ? x : y);
}
int main()
{
Compare comp(5, 4);
cout << comp.max() << endl;
cout << comp.min() << endl;
system("pause");
return 0;
}
在类模板内定义则成功:
#include"iostream"
using namespace std;
template
class Compare
{
public:
Compare(numtype a, numtype b)
{
x = a; y = b;
}
numtype max();
numtype min();
private:
numtype x, y;
};
/*template
Compare::Compare(numtype a, numpyte b)
{
x = a;
y = b;
}*/
template
numtype Compare::max()
{
return((x > y) ? x : y);
}
template
numtype Compare::min()
{
return((x < y) ? x : y);
}
int main()
{
Compare comp(5, 4);
cout << comp.max() << endl;
cout << comp.min() << endl;
system("pause");
return 0;
}
是在模板类外不能定义构造函数还是我语法错了,知道的请告诉我一声,谢谢!