我想将类模板对象传递给模板函数,该怎么做? 。我在代码中标记了错误位置。在代码块中执行时,它显示以下错误消息:
没有匹配功能可调用'swap(square<int>& , square<int>&
)'
#include<iostream>
using namespace std;
template<class U>
class square
{
U x;
U y[5];
public:
square()
{
cout<<"Enter x\n";
cin>>x;
cout<<"Enter array values\n";
for(int i=0;i<5;i++)
{
cin>>y[i];
}
}
void display()
{
cout<<endl<<"x="<<x<<endl;
cout<<"array values\n";
for(int i=0;i<5;i++)
{
cout<<" "<<y[i];
}
}
};
template<class T>
void swap(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
main()
{
square<int>A;
square<int>B;
cout<<"\nBefore swap\n";
A.display();
B.display();
swap<square>(A,B); // This shows error....why ?
cout<<"\nAfter swap\n";
A.display();
B.display();
}