请教模板的问题
#include<iostream>using namespace std;
/*template<class t1,class t2>
class Pair
{
public:
Pair(int,int);
void print(int,int);
private:
t1 first;
t2 second;
};
template<class t1,class t2>
Pair<t1,t2>::Pair(int s,int j)
{
first=s;
second=j;
print(first,second);
}
template<class t1,class t2>
void Pair<t1,t2>::print(int num1,int num2)
{
cout<<"after swap number:";
int num;
num=num1;num1=num2;num2=num;
cout<<num1<<" "<<num2<<endl;
}
void main()
{
int i,j;
cout<<"Input tow number:";
cin>>i>>j;
Pair<int,int> p(i,j);
}*/
const int N=10;
static int j=0;
bool b=false;
template<class T>
class Set
{
public:
//friend istream& operator >>(istream&, const Set&);
//friend ostream& operator <<(ostream&, const Set&);
Set();
T operator*(const Set<T>&) const;
T operator+(Set<T>&);
T& operator=(const Set<T>&);
int array[2*N];
};
template<class T>
istream& operator >>(istream& in,Set<T>& s)
{
for(int i=0;i<N;i++)
in>>s.array[i];
return in;
}
template<class T>
ostream& operator <<(ostream& out,Set<T>& s)
{
if(j==0)
{
for(int i=0;i<N;i++)
{
out<<s.array[i]<<" ";
}
}
else
{
for(int k=0;k<j;k++)
out<<s.array[k]<<" ";
j=0;
}
return out;
}
template<class T>
T& Set<T>::operator =(const Set<T>& s)
{
if(j!=0)
for(int k=0;k<j;k++)
array[k]=s.array[k];
for(int i=0;i<N;i++)
array[i]=s.array[i];
return *this;
}
template<class T>
T Set<T>::operator *(const Set<T>& s) const
{
Set<T> set;
int num=0;
if(b==false)
for(int i=0;i<N;i++)
{
for(int k=0;k<N;k++)
if(array[i]==s.array[k]){set.array[num++]=array[i];break;}
}
else
for(int i=0;i<N+1;i++)
{
for(int k=0;k<N+1;k++)
if(array[i]==s.array[k]){set.array[num++]=array[i];break;}
}
for(int l=0;l<num;l++)
{
for(int r=l+1;r<num;)
{
if(set.array[l]==set.array[r])
{
int t=r;
while(t<num){
set.array[t]=set.array[t+1];t++;
}
num--;
}
else {set.array[j]=set.array[l];r++;}
}
j++;
}
return set;
}
template<class T>
T Set<T>::operator +(Set<T>& s)
{
Set<T> list;
int i,k;
int num=0;
for(i=0;i<N;i++)
list.array[num++]=array[i];
for(k=0;k<N;k++)
list.array[num++]=s.array[k];
for(int l=0;l<num;l++)
{
for(int r=l+1;r<num;)
{
if(list.array[l]==list.array[r])
{
int t=r;
while(t<num){
list.array[t]=list.array[t+1];t++;
}
num--;
}
else {list.array[j]=list.array[l];r++;}
}
j++;
}
return list;
}
void main()
{
Set<int> t1,t2,t3;
cout<<"Init t1:";
cin>>t1;
cout<<"t1:"<<t1<<endl;
t2=t1;
cout<<"t2:"<<t2<<endl;
cout<<"t2不能和t1一样啊!要重来啊!"<<endl;
cin>>t2;
cout<<"新的t2:"<<t2<<endl;
t3=t1*t2;
cout<<"t1和t2的先前交集:"<<t3<<endl;
b=true;
t3=t1*t2;
cout<<"t1和t2的之后交集:"<<t3<<endl;
t3=t1+t2;
cout<<"t1和t2的并集:"<<t3<<endl;
}
在这里,t3=t1*t2; t3=t1+t2;总是编译不过去,请指教!!!