C++类模板,类外定义构造函数出现了问题,求解答
#include<iostream>usingnamespacestd;template<classT>classA{public://A(){x=0;y=0;cout<...
#include<iostream>
using namespace std;
template<class T>
class A
{
public:
// A(){x=0;y=0;cout<<"不带参数的构造函数"<<endl;}
A(T x,T y){}
/* {
this->x=x;this->y=y;cout<<"带一个参数的构造函数"<<endl;
}*/
void swap();
void show();
private:
T x,y;
};
template<class T>
A<T>::A(T x,T y)
{
this->x=x;this->y=y;cout<<"带一个参数的构造函数"<<endl;
}
template<class T>
void A<T>::swap()
{
T z=this->x;
this->x=this->y;
this->y=z;
}
template<class T>
void A<T>::show ()
{
cout<<"x:"<<x<<"\t"<<"y:"<<y<<endl;
}
void main()
{
A<double> a(3.3,4.4);
a.show();
a.swap();
a.show();
} 展开
using namespace std;
template<class T>
class A
{
public:
// A(){x=0;y=0;cout<<"不带参数的构造函数"<<endl;}
A(T x,T y){}
/* {
this->x=x;this->y=y;cout<<"带一个参数的构造函数"<<endl;
}*/
void swap();
void show();
private:
T x,y;
};
template<class T>
A<T>::A(T x,T y)
{
this->x=x;this->y=y;cout<<"带一个参数的构造函数"<<endl;
}
template<class T>
void A<T>::swap()
{
T z=this->x;
this->x=this->y;
this->y=z;
}
template<class T>
void A<T>::show ()
{
cout<<"x:"<<x<<"\t"<<"y:"<<y<<endl;
}
void main()
{
A<double> a(3.3,4.4);
a.show();
a.swap();
a.show();
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询