user-0 提问: c++模板问题

回复:
user: user-1
content: 引用 5 楼 qq_34524827 的回复:[quote=引用 3 楼 paschen的回复:]这样写吧：   #include<iostream> #include<string.h> using namespace std; template<class A> A sum(A *array,int size=0) { 	A total=0; 	for(int i=0;i<size;i++) 		total+=array[i]; 	return total; };   char* sum(char *array,int size=0) { 	array[size] = '\0'; 	return array; };   int main() { 	int n,int_array[100]; 	double double_array[100]; 	float float_array[100]; 	char char_array[100]; 	cout<<"int :"<<endl; 	cin>>n; 	for(int i=0;i<n;i++) 		cin>>int_array[i]; 	int itotal=sum(int_array,n); 	cout<<"int sum:"<<itotal<<endl;  	cout<<"double :"<<endl; 	cin>>n; 	for(int j=0;j<n;j++) 		cin>>double_array[j]; 	double dtotal=sum(double_array,n); 	cout<<"double sum:"<<dtotal<<endl;  	cout<<"float :"<<endl; 	cin>>n; 	for(int k=0;k<n;k++) 		cin>>float_array[k]; 	double ftotal=sum(float_array,n); 	cout<<"float sum:"<<ftotal<<endl;  	cout<<"char :"<<endl; 	cin>>n; 	for(int l=0;l<n;l++) 		cin>>char_array[l]; 	char* ctotal=sum(char_array,n); 	cout<<"char sum:"<<ctotal<<endl;  	return 0; }  运行出错好像是char*sum和模板sum重复了[/quote]  什么错误  这里是重载形式，不存在重复问题，另外重复也是编译错误，而不是运行错误

user: user-0
content: 引用 3 楼 paschen的回复:这样写吧：   #include<iostream> #include<string.h> using namespace std; template<class A> A sum(A *array,int size=0) { 	A total=0; 	for(int i=0;i<size;i++) 		total+=array[i]; 	return total; };   char* sum(char *array,int size=0) { 	array[size] = '\0'; 	return array; };   int main() { 	int n,int_array[100]; 	double double_array[100]; 	float float_array[100]; 	char char_array[100]; 	cout<<"int :"<<endl; 	cin>>n; 	for(int i=0;i<n;i++) 		cin>>int_array[i]; 	int itotal=sum(int_array,n); 	cout<<"int sum:"<<itotal<<endl;  	cout<<"double :"<<endl; 	cin>>n; 	for(int j=0;j<n;j++) 		cin>>double_array[j]; 	double dtotal=sum(double_array,n); 	cout<<"double sum:"<<dtotal<<endl;  	cout<<"float :"<<endl; 	cin>>n; 	for(int k=0;k<n;k++) 		cin>>float_array[k]; 	double ftotal=sum(float_array,n); 	cout<<"float sum:"<<ftotal<<endl;  	cout<<"char :"<<endl; 	cin>>n; 	for(int l=0;l<n;l++) 		cin>>char_array[l]; 	char* ctotal=sum(char_array,n); 	cout<<"char sum:"<<ctotal<<endl;  	return 0; }  运行出错好像是char*sum和模板sum重复了

user: user-0
content: 引用 1 楼 user-2的回复:char类型本来就和int没差别，只有库类型std::string的operator+才有你要的效果 如何实现这个效果？

user: user-1
content: 这样写吧：   #include<iostream> #include<string.h> using namespace std; template<class A> A sum(A *array,int size=0) { 	A total=0; 	for(int i=0;i<size;i++) 		total+=array[i]; 	return total; };   char* sum(char *array,int size=0) { 	array[size] = '\0'; 	return array; };   int main() { 	int n,int_array[100]; 	double double_array[100]; 	float float_array[100]; 	char char_array[100]; 	cout<<"int :"<<endl; 	cin>>n; 	for(int i=0;i<n;i++) 		cin>>int_array[i]; 	int itotal=sum(int_array,n); 	cout<<"int sum:"<<itotal<<endl;  	cout<<"double :"<<endl; 	cin>>n; 	for(int j=0;j<n;j++) 		cin>>double_array[j]; 	double dtotal=sum(double_array,n); 	cout<<"double sum:"<<dtotal<<endl;  	cout<<"float :"<<endl; 	cin>>n; 	for(int k=0;k<n;k++) 		cin>>float_array[k]; 	double ftotal=sum(float_array,n); 	cout<<"float sum:"<<ftotal<<endl;  	cout<<"char :"<<endl; 	cin>>n; 	for(int l=0;l<n;l++) 		cin>>char_array[l]; 	char* ctotal=sum(char_array,n); 	cout<<"char sum:"<<ctotal<<endl;  	return 0; }

user: user-0
content: 引用 1 楼 user-2的回复:char类型本来就和int没差别，只有库类型std::string的operator+才有你要的效果 那我要进行运算符重载吗？

user: user-2
content: char类型本来就和int没差别，只有库类型std::string的operator+才有你要的效果