永发信息网

c++ 函数模板的问题

答案:3  悬赏:10  手机版
解决时间 2021-03-08 17:58
#include <iostream>
using namespace std;
template<class Type> Type delete(Type& a[], int& n, int i);
void main()
{
int n=6;
int i,k;
int a1[6]={1,2,3,4,5,6};
cout<<"i取值范围为0~4";
cout<<"i=";
cin>>i;
int x=delete(a1,n,i);
for(k=0;k<n;k++)
cout<<a1[k]<<' ';
}

template<class Type>
Type delete(Type& a[], int& n, int i)
{
Type x;
x=a[i];
for(int j=i+1;j<n;j++)
a[j-1]=a[j];
n--;
return x;
}

f:\c++\第七章课后题\第四题\3\3\3.cpp(3) : error C2988: 不可识别的模板声明/定义
f:\c++\第七章课后题\第四题\3\3\3.cpp(3) : error C2059: 语法错误 : “delete”
f:\c++\第七章课后题\第四题\3\3\3.cpp(5) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
f:\c++\第七章课后题\第四题\3\3\3.cpp(5) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
f:\c++\第七章课后题\第四题\3\3\3.cpp(18) : error C2988: 不可识别的模板声明/定义
f:\c++\第七章课后题\第四题\3\3\3.cpp(18) : error C2059: 语法错误 : “delete”
谢谢!
最佳答案
下面程序能在我的visual studio中运行,请试试。delete是其中已经定义的标识符,不能再次定义
#include <iostream>
using namespace std;
template<class Type> Type delete1(Type a[], int& n, int i);
void main()
{
int n=6;
int i,k;
int a1[6]={1,2,3,4,5,6};
cout<<"i取值范围为0~4";
cout<<"i=";
cin>>i;
int x=delete1(a1,n,i);
for(k=0;k<n;k++)
cout<<a1[k]<<' ';
}

template<class Type>
Type delete1(Type a[], int& n, int i)
{
Type x;
x=a[i];
for(int j=i+1;j<n;j++)
a[j-1]=a[j];
n--;
return x;
}
全部回答
delete 是系统关键字,不能自定义为变量或者函数名 换个名字
和标准库里的swap重复了,加个::就可以了 #include using namespace std; template void swap(t& a,t& b) {t t=a;a=b;b=t; } int main() { double da=1.12,ds=2.13; int ia=1,is=3; cout<
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯