这个错误怎样改~~???

来源:百度知道 编辑:UC知道 时间:2024/05/23 18:21:08
#include<iostream.h>
template<class Type> void sort(Type *A,int n){
Type t;
void swapl(Type *a,Type *b){
type temp;
temp=*a;
*a=*b;
*b=temp;
}
void sort(Type *A,int n){
int i,j,index;
type elem;
for(i=0;i<n-1;i++){
elem=*(A+i);
index=i;
for(j=i+1;j<n;j++)
if(*(A=j)>elem){
elem=*(A+j);
index=j;
}
t=swapl(A+i,A+index);
}
}
return t;
}

main()
{
int a1[5]={78,8,96,28,31};
cout<<sort(a1,5)<<endl;
}
只有一个错误
d:\c++\chapter7_4\pro7-4.cpp(31) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

这个程序怎样改???

#include<iostream.h>

template<class Type>
void sort(Type *A,int n);

template<class Type>
void swapl(Type *a,Type *b)
{
Type temp;
temp=*a;
*a=*b;
*b=temp;
}

template<class Type>
void sort(Type *A,int n)
{
int i,j,index;
Type elem;
for(i=0;i<n-1;i++){
elem=*(A+i);
index=i;
for(j=i+1;j<n;j++)
if(*(A+j)>elem)
{
elem=*(A+j);
index=j;
}
swapl(A+i,A+index);
}
}

int main()
{
int a1[5]={78,8,96,28,31};
sort(a1,5);
for (int i=0;i<5;i++)
cout<<a1[i]<<" ";
cout<<endl;
return 0;
}

模板函数在每个函数前都有一个模板声明的.
没有返回值的不能用于cout的对象.
大小写要注意呀,Type 跟 type是不一样的.

你的sort()返回值为空,怎么能拿来放入输出流呢