求教一个函数模板不能实例化的有关问题(有代码)
求教一个函数模板不能实例化的问题(有代码)
Perm中的swap函数不能实例化//////////////
#include <iostream>
using namespace std;
template <class T>
inline void swap(T& a,T& b)
{
T temp=a;a=b;b=temp;
}
template <class T>
void Perm(T list[],int k,int m)
{
int i;
if(k==m)
{
for(i=0;i <=m;i++)
cout < <list[i];
cout < <endl;
}
else
for(i=k;i <=m;i++)
{
swap(list[k],list[i]);////
Perm(list,k+1,m);
swap(list[k],list[i]);////
}
}
void main()
{
char a[10]={ 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j '};
Perm(a,0,9);
}
------解决方案--------------------
swap和std里的标准函数同名
你改个名字?swapNum
------解决方案--------------------
或者swap函数改名
或者去掉using namespace std,在函数里cout和endl之前指定std名字空间
或者使用使用声明而不是使用指示,如下:
#include <iostream>
template <class T>
inline void swap(T& a,T& b)
{
T temp=a;a=b;b=temp;
}
template <class T>
void Perm(T list[],int k,int m)
{
using std::cout;
using std::endl;
int i;
if(k==m)
{
for(i=0;i <=m;i++)
cout < <list[i];
cout < <endl;
}
else
for(i=k;i <=m;i++)
{
swap(list[k],list[i]);////
Perm(list,k+1,m);
swap(list[k],list[i]);////
}
}
void main()
{
char a[10]={ 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j '};
Perm(a,0,9);
}
Perm中的swap函数不能实例化//////////////
#include <iostream>
using namespace std;
template <class T>
inline void swap(T& a,T& b)
{
T temp=a;a=b;b=temp;
}
template <class T>
void Perm(T list[],int k,int m)
{
int i;
if(k==m)
{
for(i=0;i <=m;i++)
cout < <list[i];
cout < <endl;
}
else
for(i=k;i <=m;i++)
{
swap(list[k],list[i]);////
Perm(list,k+1,m);
swap(list[k],list[i]);////
}
}
void main()
{
char a[10]={ 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j '};
Perm(a,0,9);
}
------解决方案--------------------
swap和std里的标准函数同名
你改个名字?swapNum
------解决方案--------------------
或者swap函数改名
或者去掉using namespace std,在函数里cout和endl之前指定std名字空间
或者使用使用声明而不是使用指示,如下:
#include <iostream>
template <class T>
inline void swap(T& a,T& b)
{
T temp=a;a=b;b=temp;
}
template <class T>
void Perm(T list[],int k,int m)
{
using std::cout;
using std::endl;
int i;
if(k==m)
{
for(i=0;i <=m;i++)
cout < <list[i];
cout < <endl;
}
else
for(i=k;i <=m;i++)
{
swap(list[k],list[i]);////
Perm(list,k+1,m);
swap(list[k],list[i]);////
}
}
void main()
{
char a[10]={ 'a ', 'b ', 'c ', 'd ', 'e ', 'f ', 'g ', 'h ', 'i ', 'j '};
Perm(a,0,9);
}